blob: d83e93b74f6eb06642ac89790cbd6b40f33510c3 [file] [log] [blame]
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +01001/*
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
Haavard Skinnemoen8dda4e62007-10-29 13:23:33 +010025#include <asm/arch/chip-features.h>
Haavard Skinnemoen1cbd2f02008-08-31 18:05:32 +020026#include <asm/arch/portmux.h>
Haavard Skinnemoen8dda4e62007-10-29 13:23:33 +010027
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010028#ifdef CONFIG_PLL
Haavard Skinnemoen77bd3672008-12-17 16:53:07 +010029#define PLL0_RATE ((CONFIG_SYS_OSC0_HZ / CONFIG_SYS_PLL0_DIV) \
30 * CONFIG_SYS_PLL0_MUL)
Haavard Skinnemoen1cbd2f02008-08-31 18:05:32 +020031#define MAIN_CLK_RATE PLL0_RATE
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010032#else
Haavard Skinnemoen77bd3672008-12-17 16:53:07 +010033#define MAIN_CLK_RATE (CONFIG_SYS_OSC0_HZ)
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010034#endif
35
36static inline unsigned long get_cpu_clk_rate(void)
37{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_CPU;
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010039}
40static inline unsigned long get_hsb_clk_rate(void)
41{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020042 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB;
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010043}
44static inline unsigned long get_pba_clk_rate(void)
45{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBA;
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010047}
48static inline unsigned long get_pbb_clk_rate(void)
49{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBB;
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010051}
52
53/* Accessors for specific devices. More will be added as needed. */
54static inline unsigned long get_sdram_clk_rate(void)
55{
56 return get_hsb_clk_rate();
57}
Haavard Skinnemoen8dda4e62007-10-29 13:23:33 +010058#ifdef AT32AP700x_CHIP_HAS_USART
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010059static inline unsigned long get_usart_clk_rate(unsigned int dev_id)
60{
61 return get_pba_clk_rate();
62}
Haavard Skinnemoen8dda4e62007-10-29 13:23:33 +010063#endif
Haavard Skinnemoen7c274182008-04-30 13:09:56 +020064#ifdef AT32AP700x_CHIP_HAS_MACB
Haavard Skinnemoena5ca9982006-12-17 16:56:14 +010065static inline unsigned long get_macb_pclk_rate(unsigned int dev_id)
66{
67 return get_pbb_clk_rate();
68}
69static inline unsigned long get_macb_hclk_rate(unsigned int dev_id)
70{
71 return get_hsb_clk_rate();
72}
Haavard Skinnemoen8dda4e62007-10-29 13:23:33 +010073#endif
74#ifdef AT32AP700x_CHIP_HAS_MMCI
Haavard Skinnemoenb950ebc2006-12-17 18:55:37 +010075static inline unsigned long get_mci_clk_rate(void)
76{
77 return get_pbb_clk_rate();
78}
Haavard Skinnemoen8dda4e62007-10-29 13:23:33 +010079#endif
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020080#ifdef AT32AP700x_CHIP_HAS_SPI
81static inline unsigned long get_spi_clk_rate(unsigned int dev_id)
82{
83 return get_pba_clk_rate();
84}
85#endif
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010086
Haavard Skinnemoen546f9542008-05-02 15:21:40 +020087extern void clk_init(void);
88
Haavard Skinnemoend5d6ca62008-01-23 17:20:14 +010089/* Board code may need the SDRAM base clock as a compile-time constant */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020090#define SDRAMC_BUS_HZ (MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB)
Haavard Skinnemoend5d6ca62008-01-23 17:20:14 +010091
Haavard Skinnemoen1cbd2f02008-08-31 18:05:32 +020092/* Generic clock control */
93enum gclk_parent {
94 GCLK_PARENT_OSC0 = 0,
95 GCLK_PARENT_OSC1 = 1,
96 GCLK_PARENT_PLL0 = 2,
97 GCLK_PARENT_PLL1 = 3,
98};
99
100/* Some generic clocks have specific roles */
101#define GCLK_DAC_SAMPLE_CLK 6
102#define GCLK_LCDC_PIXCLK 7
103
104extern unsigned long __gclk_set_rate(unsigned int id, enum gclk_parent parent,
105 unsigned long rate, unsigned long parent_rate);
106
107/**
108 * gclk_set_rate - configure and enable a generic clock
109 * @id: Which GCLK[id] to enable
110 * @parent: Parent clock feeding the GCLK
111 * @rate: Target rate of the GCLK in Hz
112 *
113 * Returns the actual GCLK rate in Hz, after rounding to the nearest
114 * supported rate.
115 *
116 * All three parameters are usually constant, hence the inline.
117 */
118static inline unsigned long gclk_set_rate(unsigned int id,
119 enum gclk_parent parent, unsigned long rate)
120{
121 unsigned long parent_rate;
122
123 if (id > 7)
124 return 0;
125
126 switch (parent) {
127 case GCLK_PARENT_OSC0:
Haavard Skinnemoen77bd3672008-12-17 16:53:07 +0100128 parent_rate = CONFIG_SYS_OSC0_HZ;
Haavard Skinnemoen1cbd2f02008-08-31 18:05:32 +0200129 break;
Haavard Skinnemoen77bd3672008-12-17 16:53:07 +0100130#ifdef CONFIG_SYS_OSC1_HZ
Haavard Skinnemoen1cbd2f02008-08-31 18:05:32 +0200131 case GCLK_PARENT_OSC1:
Haavard Skinnemoen77bd3672008-12-17 16:53:07 +0100132 parent_rate = CONFIG_SYS_OSC1_HZ;
Haavard Skinnemoen1cbd2f02008-08-31 18:05:32 +0200133 break;
134#endif
135#ifdef PLL0_RATE
136 case GCLK_PARENT_PLL0:
137 parent_rate = PLL0_RATE;
138 break;
139#endif
140#ifdef PLL1_RATE
141 case GCLK_PARENT_PLL1:
142 parent_rate = PLL1_RATE;
143 break;
144#endif
145 default:
146 parent_rate = 0;
147 break;
148 }
149
150 return __gclk_set_rate(id, parent, rate, parent_rate);
151}
152
153/**
154 * gclk_enable_output - enable output on a GCLK pin
155 * @id: Which GCLK[id] pin to enable
156 * @drive_strength: Drive strength of external GCLK pin, if applicable
157 */
158static inline void gclk_enable_output(unsigned int id,
159 unsigned long drive_strength)
160{
161 switch (id) {
162 case 0:
163 portmux_select_peripheral(PORTMUX_PORT_A, 1 << 30,
164 PORTMUX_FUNC_A, drive_strength);
165 break;
166 case 1:
167 portmux_select_peripheral(PORTMUX_PORT_A, 1 << 31,
168 PORTMUX_FUNC_A, drive_strength);
169 break;
170 case 2:
171 portmux_select_peripheral(PORTMUX_PORT_B, 1 << 19,
172 PORTMUX_FUNC_A, drive_strength);
173 break;
174 case 3:
175 portmux_select_peripheral(PORTMUX_PORT_B, 1 << 29,
176 PORTMUX_FUNC_A, drive_strength);
177 break;
178 case 4:
179 portmux_select_peripheral(PORTMUX_PORT_B, 1 << 30,
180 PORTMUX_FUNC_A, drive_strength);
181 break;
182 }
183}
184
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +0100185#endif /* __ASM_AVR32_ARCH_CLK_H__ */