]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - include/asm-avr32/arch-at32ap700x/clk.h
avr32: Add support for "GPIO" port mux
[karo-tx-uboot.git] / include / asm-avr32 / arch-at32ap700x / clk.h
1 /*
2  * Copyright (C) 2006 Atmel Corporation
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 #ifndef __ASM_AVR32_ARCH_CLK_H__
23 #define __ASM_AVR32_ARCH_CLK_H__
24
25 #include <asm/arch/chip-features.h>
26 #include <asm/arch/portmux.h>
27
28 #ifdef CONFIG_PLL
29 #define PLL0_RATE       ((CFG_OSC0_HZ / CFG_PLL0_DIV) * CFG_PLL0_MUL)
30 #define MAIN_CLK_RATE   PLL0_RATE
31 #else
32 #define MAIN_CLK_RATE   (CFG_OSC0_HZ)
33 #endif
34
35 static inline unsigned long get_cpu_clk_rate(void)
36 {
37         return MAIN_CLK_RATE >> CFG_CLKDIV_CPU;
38 }
39 static inline unsigned long get_hsb_clk_rate(void)
40 {
41         return MAIN_CLK_RATE >> CFG_CLKDIV_HSB;
42 }
43 static inline unsigned long get_pba_clk_rate(void)
44 {
45         return MAIN_CLK_RATE >> CFG_CLKDIV_PBA;
46 }
47 static inline unsigned long get_pbb_clk_rate(void)
48 {
49         return MAIN_CLK_RATE >> CFG_CLKDIV_PBB;
50 }
51
52 /* Accessors for specific devices. More will be added as needed. */
53 static inline unsigned long get_sdram_clk_rate(void)
54 {
55         return get_hsb_clk_rate();
56 }
57 #ifdef AT32AP700x_CHIP_HAS_USART
58 static inline unsigned long get_usart_clk_rate(unsigned int dev_id)
59 {
60         return get_pba_clk_rate();
61 }
62 #endif
63 #ifdef AT32AP700x_CHIP_HAS_MACB
64 static inline unsigned long get_macb_pclk_rate(unsigned int dev_id)
65 {
66         return get_pbb_clk_rate();
67 }
68 static inline unsigned long get_macb_hclk_rate(unsigned int dev_id)
69 {
70         return get_hsb_clk_rate();
71 }
72 #endif
73 #ifdef AT32AP700x_CHIP_HAS_MMCI
74 static inline unsigned long get_mci_clk_rate(void)
75 {
76         return get_pbb_clk_rate();
77 }
78 #endif
79 #ifdef AT32AP700x_CHIP_HAS_SPI
80 static inline unsigned long get_spi_clk_rate(unsigned int dev_id)
81 {
82         return get_pba_clk_rate();
83 }
84 #endif
85
86 extern void clk_init(void);
87
88 /* Board code may need the SDRAM base clock as a compile-time constant */
89 #define SDRAMC_BUS_HZ   (MAIN_CLK_RATE >> CFG_CLKDIV_HSB)
90
91 /* Generic clock control */
92 enum gclk_parent {
93         GCLK_PARENT_OSC0 = 0,
94         GCLK_PARENT_OSC1 = 1,
95         GCLK_PARENT_PLL0 = 2,
96         GCLK_PARENT_PLL1 = 3,
97 };
98
99 /* Some generic clocks have specific roles */
100 #define GCLK_DAC_SAMPLE_CLK     6
101 #define GCLK_LCDC_PIXCLK        7
102
103 extern unsigned long __gclk_set_rate(unsigned int id, enum gclk_parent parent,
104                 unsigned long rate, unsigned long parent_rate);
105
106 /**
107  * gclk_set_rate - configure and enable a generic clock
108  * @id: Which GCLK[id] to enable
109  * @parent: Parent clock feeding the GCLK
110  * @rate: Target rate of the GCLK in Hz
111  *
112  * Returns the actual GCLK rate in Hz, after rounding to the nearest
113  * supported rate.
114  *
115  * All three parameters are usually constant, hence the inline.
116  */
117 static inline unsigned long gclk_set_rate(unsigned int id,
118                 enum gclk_parent parent, unsigned long rate)
119 {
120         unsigned long parent_rate;
121
122         if (id > 7)
123                 return 0;
124
125         switch (parent) {
126         case GCLK_PARENT_OSC0:
127                 parent_rate = CFG_OSC0_HZ;
128                 break;
129 #ifdef CFG_OSC1_HZ
130         case GCLK_PARENT_OSC1:
131                 parent_rate = CFG_OSC1_HZ;
132                 break;
133 #endif
134 #ifdef PLL0_RATE
135         case GCLK_PARENT_PLL0:
136                 parent_rate = PLL0_RATE;
137                 break;
138 #endif
139 #ifdef PLL1_RATE
140         case GCLK_PARENT_PLL1:
141                 parent_rate = PLL1_RATE;
142                 break;
143 #endif
144         default:
145                 parent_rate = 0;
146                 break;
147         }
148
149         return __gclk_set_rate(id, parent, rate, parent_rate);
150 }
151
152 /**
153  * gclk_enable_output - enable output on a GCLK pin
154  * @id: Which GCLK[id] pin to enable
155  * @drive_strength: Drive strength of external GCLK pin, if applicable
156  */
157 static inline void gclk_enable_output(unsigned int id,
158                 unsigned long drive_strength)
159 {
160         switch (id) {
161         case 0:
162                 portmux_select_peripheral(PORTMUX_PORT_A, 1 << 30,
163                                 PORTMUX_FUNC_A, drive_strength);
164                 break;
165         case 1:
166                 portmux_select_peripheral(PORTMUX_PORT_A, 1 << 31,
167                                 PORTMUX_FUNC_A, drive_strength);
168                 break;
169         case 2:
170                 portmux_select_peripheral(PORTMUX_PORT_B, 1 << 19,
171                                 PORTMUX_FUNC_A, drive_strength);
172                 break;
173         case 3:
174                 portmux_select_peripheral(PORTMUX_PORT_B, 1 << 29,
175                                 PORTMUX_FUNC_A, drive_strength);
176                 break;
177         case 4:
178                 portmux_select_peripheral(PORTMUX_PORT_B, 1 << 30,
179                                 PORTMUX_FUNC_A, drive_strength);
180                 break;
181         }
182 }
183
184 #endif /* __ASM_AVR32_ARCH_CLK_H__ */