Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Atmel Corporation |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 5 | */ |
| 6 | #ifndef __ASM_AVR32_ARCH_CLK_H__ |
| 7 | #define __ASM_AVR32_ARCH_CLK_H__ |
| 8 | |
Haavard Skinnemoen | 8dda4e6 | 2007-10-29 13:23:33 +0100 | [diff] [blame] | 9 | #include <asm/arch/chip-features.h> |
Haavard Skinnemoen | 1cbd2f0 | 2008-08-31 18:05:32 +0200 | [diff] [blame] | 10 | #include <asm/arch/portmux.h> |
Haavard Skinnemoen | 8dda4e6 | 2007-10-29 13:23:33 +0100 | [diff] [blame] | 11 | |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 12 | #ifdef CONFIG_PLL |
Haavard Skinnemoen | 77bd367 | 2008-12-17 16:53:07 +0100 | [diff] [blame] | 13 | #define PLL0_RATE ((CONFIG_SYS_OSC0_HZ / CONFIG_SYS_PLL0_DIV) \ |
| 14 | * CONFIG_SYS_PLL0_MUL) |
Haavard Skinnemoen | 1cbd2f0 | 2008-08-31 18:05:32 +0200 | [diff] [blame] | 15 | #define MAIN_CLK_RATE PLL0_RATE |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 16 | #else |
Haavard Skinnemoen | 77bd367 | 2008-12-17 16:53:07 +0100 | [diff] [blame] | 17 | #define MAIN_CLK_RATE (CONFIG_SYS_OSC0_HZ) |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | static inline unsigned long get_cpu_clk_rate(void) |
| 21 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 22 | return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_CPU; |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 23 | } |
| 24 | static inline unsigned long get_hsb_clk_rate(void) |
| 25 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB; |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 27 | } |
| 28 | static inline unsigned long get_pba_clk_rate(void) |
| 29 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 30 | return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBA; |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 31 | } |
| 32 | static inline unsigned long get_pbb_clk_rate(void) |
| 33 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 34 | return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBB; |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* Accessors for specific devices. More will be added as needed. */ |
| 38 | static inline unsigned long get_sdram_clk_rate(void) |
| 39 | { |
| 40 | return get_hsb_clk_rate(); |
| 41 | } |
Haavard Skinnemoen | 8dda4e6 | 2007-10-29 13:23:33 +0100 | [diff] [blame] | 42 | #ifdef AT32AP700x_CHIP_HAS_USART |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 43 | static inline unsigned long get_usart_clk_rate(unsigned int dev_id) |
| 44 | { |
| 45 | return get_pba_clk_rate(); |
| 46 | } |
Haavard Skinnemoen | 8dda4e6 | 2007-10-29 13:23:33 +0100 | [diff] [blame] | 47 | #endif |
Haavard Skinnemoen | 7c27418 | 2008-04-30 13:09:56 +0200 | [diff] [blame] | 48 | #ifdef AT32AP700x_CHIP_HAS_MACB |
Haavard Skinnemoen | a5ca998 | 2006-12-17 16:56:14 +0100 | [diff] [blame] | 49 | static inline unsigned long get_macb_pclk_rate(unsigned int dev_id) |
| 50 | { |
| 51 | return get_pbb_clk_rate(); |
| 52 | } |
| 53 | static inline unsigned long get_macb_hclk_rate(unsigned int dev_id) |
| 54 | { |
| 55 | return get_hsb_clk_rate(); |
| 56 | } |
Haavard Skinnemoen | 8dda4e6 | 2007-10-29 13:23:33 +0100 | [diff] [blame] | 57 | #endif |
| 58 | #ifdef AT32AP700x_CHIP_HAS_MMCI |
Haavard Skinnemoen | b950ebc | 2006-12-17 18:55:37 +0100 | [diff] [blame] | 59 | static inline unsigned long get_mci_clk_rate(void) |
| 60 | { |
| 61 | return get_pbb_clk_rate(); |
| 62 | } |
Haavard Skinnemoen | 8dda4e6 | 2007-10-29 13:23:33 +0100 | [diff] [blame] | 63 | #endif |
Hans-Christian Egtvedt | 9b4381b | 2008-05-16 11:10:32 +0200 | [diff] [blame] | 64 | #ifdef AT32AP700x_CHIP_HAS_SPI |
| 65 | static inline unsigned long get_spi_clk_rate(unsigned int dev_id) |
| 66 | { |
| 67 | return get_pba_clk_rate(); |
| 68 | } |
| 69 | #endif |
Mark Jackson | c563e48 | 2009-07-21 11:11:37 +0100 | [diff] [blame] | 70 | #ifdef AT32AP700x_CHIP_HAS_LCDC |
| 71 | static inline unsigned long get_lcdc_clk_rate(unsigned int dev_id) |
| 72 | { |
| 73 | return get_hsb_clk_rate(); |
| 74 | } |
| 75 | #endif |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 76 | |
Haavard Skinnemoen | 546f954 | 2008-05-02 15:21:40 +0200 | [diff] [blame] | 77 | extern void clk_init(void); |
| 78 | |
Haavard Skinnemoen | d5d6ca6 | 2008-01-23 17:20:14 +0100 | [diff] [blame] | 79 | /* Board code may need the SDRAM base clock as a compile-time constant */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 80 | #define SDRAMC_BUS_HZ (MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB) |
Haavard Skinnemoen | d5d6ca6 | 2008-01-23 17:20:14 +0100 | [diff] [blame] | 81 | |
Haavard Skinnemoen | 1cbd2f0 | 2008-08-31 18:05:32 +0200 | [diff] [blame] | 82 | /* Generic clock control */ |
| 83 | enum gclk_parent { |
| 84 | GCLK_PARENT_OSC0 = 0, |
| 85 | GCLK_PARENT_OSC1 = 1, |
| 86 | GCLK_PARENT_PLL0 = 2, |
| 87 | GCLK_PARENT_PLL1 = 3, |
| 88 | }; |
| 89 | |
| 90 | /* Some generic clocks have specific roles */ |
| 91 | #define GCLK_DAC_SAMPLE_CLK 6 |
| 92 | #define GCLK_LCDC_PIXCLK 7 |
| 93 | |
| 94 | extern unsigned long __gclk_set_rate(unsigned int id, enum gclk_parent parent, |
| 95 | unsigned long rate, unsigned long parent_rate); |
| 96 | |
| 97 | /** |
| 98 | * gclk_set_rate - configure and enable a generic clock |
| 99 | * @id: Which GCLK[id] to enable |
| 100 | * @parent: Parent clock feeding the GCLK |
| 101 | * @rate: Target rate of the GCLK in Hz |
| 102 | * |
| 103 | * Returns the actual GCLK rate in Hz, after rounding to the nearest |
| 104 | * supported rate. |
| 105 | * |
| 106 | * All three parameters are usually constant, hence the inline. |
| 107 | */ |
| 108 | static inline unsigned long gclk_set_rate(unsigned int id, |
| 109 | enum gclk_parent parent, unsigned long rate) |
| 110 | { |
| 111 | unsigned long parent_rate; |
| 112 | |
| 113 | if (id > 7) |
| 114 | return 0; |
| 115 | |
| 116 | switch (parent) { |
| 117 | case GCLK_PARENT_OSC0: |
Haavard Skinnemoen | 77bd367 | 2008-12-17 16:53:07 +0100 | [diff] [blame] | 118 | parent_rate = CONFIG_SYS_OSC0_HZ; |
Haavard Skinnemoen | 1cbd2f0 | 2008-08-31 18:05:32 +0200 | [diff] [blame] | 119 | break; |
Haavard Skinnemoen | 77bd367 | 2008-12-17 16:53:07 +0100 | [diff] [blame] | 120 | #ifdef CONFIG_SYS_OSC1_HZ |
Haavard Skinnemoen | 1cbd2f0 | 2008-08-31 18:05:32 +0200 | [diff] [blame] | 121 | case GCLK_PARENT_OSC1: |
Haavard Skinnemoen | 77bd367 | 2008-12-17 16:53:07 +0100 | [diff] [blame] | 122 | parent_rate = CONFIG_SYS_OSC1_HZ; |
Haavard Skinnemoen | 1cbd2f0 | 2008-08-31 18:05:32 +0200 | [diff] [blame] | 123 | break; |
| 124 | #endif |
| 125 | #ifdef PLL0_RATE |
| 126 | case GCLK_PARENT_PLL0: |
| 127 | parent_rate = PLL0_RATE; |
| 128 | break; |
| 129 | #endif |
| 130 | #ifdef PLL1_RATE |
| 131 | case GCLK_PARENT_PLL1: |
| 132 | parent_rate = PLL1_RATE; |
| 133 | break; |
| 134 | #endif |
| 135 | default: |
| 136 | parent_rate = 0; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | return __gclk_set_rate(id, parent, rate, parent_rate); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * gclk_enable_output - enable output on a GCLK pin |
| 145 | * @id: Which GCLK[id] pin to enable |
| 146 | * @drive_strength: Drive strength of external GCLK pin, if applicable |
| 147 | */ |
| 148 | static inline void gclk_enable_output(unsigned int id, |
| 149 | unsigned long drive_strength) |
| 150 | { |
| 151 | switch (id) { |
| 152 | case 0: |
| 153 | portmux_select_peripheral(PORTMUX_PORT_A, 1 << 30, |
| 154 | PORTMUX_FUNC_A, drive_strength); |
| 155 | break; |
| 156 | case 1: |
| 157 | portmux_select_peripheral(PORTMUX_PORT_A, 1 << 31, |
| 158 | PORTMUX_FUNC_A, drive_strength); |
| 159 | break; |
| 160 | case 2: |
| 161 | portmux_select_peripheral(PORTMUX_PORT_B, 1 << 19, |
| 162 | PORTMUX_FUNC_A, drive_strength); |
| 163 | break; |
| 164 | case 3: |
| 165 | portmux_select_peripheral(PORTMUX_PORT_B, 1 << 29, |
| 166 | PORTMUX_FUNC_A, drive_strength); |
| 167 | break; |
| 168 | case 4: |
| 169 | portmux_select_peripheral(PORTMUX_PORT_B, 1 << 30, |
| 170 | PORTMUX_FUNC_A, drive_strength); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 175 | #endif /* __ASM_AVR32_ARCH_CLK_H__ */ |