blob: 0721899fde5ef4605552e653d5cb6f12d82d9b64 [file] [log] [blame]
Haavard Skinnemoendf548d32006-11-19 18:06:53 +01001/*
2 * Copyright (C) 2006 Atmel Corporation
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoendf548d32006-11-19 18:06:53 +01005 */
6#ifndef __ASM_AVR32_ARCH_CLK_H__
7#define __ASM_AVR32_ARCH_CLK_H__
8
Haavard Skinnemoen5fee84a2007-10-29 13:23:33 +01009#include <asm/arch/chip-features.h>
Haavard Skinnemoen98090cd2008-08-31 18:05:32 +020010#include <asm/arch/portmux.h>
Haavard Skinnemoen5fee84a2007-10-29 13:23:33 +010011
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010012#ifdef CONFIG_PLL
Haavard Skinnemoencb547322008-12-17 16:53:07 +010013#define PLL0_RATE ((CONFIG_SYS_OSC0_HZ / CONFIG_SYS_PLL0_DIV) \
14 * CONFIG_SYS_PLL0_MUL)
Haavard Skinnemoen98090cd2008-08-31 18:05:32 +020015#define MAIN_CLK_RATE PLL0_RATE
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010016#else
Haavard Skinnemoencb547322008-12-17 16:53:07 +010017#define MAIN_CLK_RATE (CONFIG_SYS_OSC0_HZ)
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010018#endif
19
20static inline unsigned long get_cpu_clk_rate(void)
21{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020022 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_CPU;
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010023}
24static inline unsigned long get_hsb_clk_rate(void)
25{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB;
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010027}
28static inline unsigned long get_pba_clk_rate(void)
29{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBA;
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010031}
32static inline unsigned long get_pbb_clk_rate(void)
33{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020034 return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBB;
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010035}
36
37/* Accessors for specific devices. More will be added as needed. */
38static inline unsigned long get_sdram_clk_rate(void)
39{
40 return get_hsb_clk_rate();
41}
Haavard Skinnemoen5fee84a2007-10-29 13:23:33 +010042#ifdef AT32AP700x_CHIP_HAS_USART
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010043static inline unsigned long get_usart_clk_rate(unsigned int dev_id)
44{
45 return get_pba_clk_rate();
46}
Haavard Skinnemoen5fee84a2007-10-29 13:23:33 +010047#endif
Haavard Skinnemoencdd42c02008-04-30 13:09:56 +020048#ifdef AT32AP700x_CHIP_HAS_MACB
Haavard Skinnemoenb4ec9c22006-12-17 16:56:14 +010049static inline unsigned long get_macb_pclk_rate(unsigned int dev_id)
50{
51 return get_pbb_clk_rate();
52}
53static inline unsigned long get_macb_hclk_rate(unsigned int dev_id)
54{
55 return get_hsb_clk_rate();
56}
Haavard Skinnemoen5fee84a2007-10-29 13:23:33 +010057#endif
58#ifdef AT32AP700x_CHIP_HAS_MMCI
Haavard Skinnemoen05fdab12006-12-17 18:55:37 +010059static inline unsigned long get_mci_clk_rate(void)
60{
61 return get_pbb_clk_rate();
62}
Haavard Skinnemoen5fee84a2007-10-29 13:23:33 +010063#endif
Hans-Christian Egtvedt60445cb2008-05-16 11:10:32 +020064#ifdef AT32AP700x_CHIP_HAS_SPI
65static inline unsigned long get_spi_clk_rate(unsigned int dev_id)
66{
67 return get_pba_clk_rate();
68}
69#endif
Mark Jackson716ece12009-07-21 11:11:37 +010070#ifdef AT32AP700x_CHIP_HAS_LCDC
71static inline unsigned long get_lcdc_clk_rate(unsigned int dev_id)
72{
73 return get_hsb_clk_rate();
74}
75#endif
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010076
Haavard Skinnemoen3ace2522008-05-02 15:21:40 +020077extern void clk_init(void);
78
Haavard Skinnemoend38da532008-01-23 17:20:14 +010079/* Board code may need the SDRAM base clock as a compile-time constant */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080#define SDRAMC_BUS_HZ (MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB)
Haavard Skinnemoend38da532008-01-23 17:20:14 +010081
Haavard Skinnemoen98090cd2008-08-31 18:05:32 +020082/* Generic clock control */
83enum 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
94extern 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 */
108static 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 Skinnemoencb547322008-12-17 16:53:07 +0100118 parent_rate = CONFIG_SYS_OSC0_HZ;
Haavard Skinnemoen98090cd2008-08-31 18:05:32 +0200119 break;
Haavard Skinnemoencb547322008-12-17 16:53:07 +0100120#ifdef CONFIG_SYS_OSC1_HZ
Haavard Skinnemoen98090cd2008-08-31 18:05:32 +0200121 case GCLK_PARENT_OSC1:
Haavard Skinnemoencb547322008-12-17 16:53:07 +0100122 parent_rate = CONFIG_SYS_OSC1_HZ;
Haavard Skinnemoen98090cd2008-08-31 18:05:32 +0200123 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 */
148static 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 Skinnemoendf548d32006-11-19 18:06:53 +0100175#endif /* __ASM_AVR32_ARCH_CLK_H__ */