clk.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <asm/arch/chip-features.h>
  25. #include <asm/arch/portmux.h>
  26. #ifdef CONFIG_PLL
  27. #define PLL0_RATE ((CONFIG_SYS_OSC0_HZ / CONFIG_SYS_PLL0_DIV) \
  28. * CONFIG_SYS_PLL0_MUL)
  29. #define MAIN_CLK_RATE PLL0_RATE
  30. #else
  31. #define MAIN_CLK_RATE (CONFIG_SYS_OSC0_HZ)
  32. #endif
  33. static inline unsigned long get_cpu_clk_rate(void)
  34. {
  35. return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_CPU;
  36. }
  37. static inline unsigned long get_hsb_clk_rate(void)
  38. {
  39. return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB;
  40. }
  41. static inline unsigned long get_pba_clk_rate(void)
  42. {
  43. return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBA;
  44. }
  45. static inline unsigned long get_pbb_clk_rate(void)
  46. {
  47. return MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_PBB;
  48. }
  49. /* Accessors for specific devices. More will be added as needed. */
  50. static inline unsigned long get_sdram_clk_rate(void)
  51. {
  52. return get_hsb_clk_rate();
  53. }
  54. #ifdef AT32AP700x_CHIP_HAS_USART
  55. static inline unsigned long get_usart_clk_rate(unsigned int dev_id)
  56. {
  57. return get_pba_clk_rate();
  58. }
  59. #endif
  60. #ifdef AT32AP700x_CHIP_HAS_MACB
  61. static inline unsigned long get_macb_pclk_rate(unsigned int dev_id)
  62. {
  63. return get_pbb_clk_rate();
  64. }
  65. static inline unsigned long get_macb_hclk_rate(unsigned int dev_id)
  66. {
  67. return get_hsb_clk_rate();
  68. }
  69. #endif
  70. #ifdef AT32AP700x_CHIP_HAS_MMCI
  71. static inline unsigned long get_mci_clk_rate(void)
  72. {
  73. return get_pbb_clk_rate();
  74. }
  75. #endif
  76. #ifdef AT32AP700x_CHIP_HAS_SPI
  77. static inline unsigned long get_spi_clk_rate(unsigned int dev_id)
  78. {
  79. return get_pba_clk_rate();
  80. }
  81. #endif
  82. extern void clk_init(void);
  83. /* Board code may need the SDRAM base clock as a compile-time constant */
  84. #define SDRAMC_BUS_HZ (MAIN_CLK_RATE >> CONFIG_SYS_CLKDIV_HSB)
  85. /* Generic clock control */
  86. enum gclk_parent {
  87. GCLK_PARENT_OSC0 = 0,
  88. GCLK_PARENT_OSC1 = 1,
  89. GCLK_PARENT_PLL0 = 2,
  90. GCLK_PARENT_PLL1 = 3,
  91. };
  92. /* Some generic clocks have specific roles */
  93. #define GCLK_DAC_SAMPLE_CLK 6
  94. #define GCLK_LCDC_PIXCLK 7
  95. extern unsigned long __gclk_set_rate(unsigned int id, enum gclk_parent parent,
  96. unsigned long rate, unsigned long parent_rate);
  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. if (id > 7)
  113. return 0;
  114. switch (parent) {
  115. case GCLK_PARENT_OSC0:
  116. parent_rate = CONFIG_SYS_OSC0_HZ;
  117. break;
  118. #ifdef CONFIG_SYS_OSC1_HZ
  119. case GCLK_PARENT_OSC1:
  120. parent_rate = CONFIG_SYS_OSC1_HZ;
  121. break;
  122. #endif
  123. #ifdef PLL0_RATE
  124. case GCLK_PARENT_PLL0:
  125. parent_rate = PLL0_RATE;
  126. break;
  127. #endif
  128. #ifdef PLL1_RATE
  129. case GCLK_PARENT_PLL1:
  130. parent_rate = PLL1_RATE;
  131. break;
  132. #endif
  133. default:
  134. parent_rate = 0;
  135. break;
  136. }
  137. return __gclk_set_rate(id, parent, rate, parent_rate);
  138. }
  139. /**
  140. * gclk_enable_output - enable output on a GCLK pin
  141. * @id: Which GCLK[id] pin to enable
  142. * @drive_strength: Drive strength of external GCLK pin, if applicable
  143. */
  144. static inline void gclk_enable_output(unsigned int id,
  145. unsigned long drive_strength)
  146. {
  147. switch (id) {
  148. case 0:
  149. portmux_select_peripheral(PORTMUX_PORT_A, 1 << 30,
  150. PORTMUX_FUNC_A, drive_strength);
  151. break;
  152. case 1:
  153. portmux_select_peripheral(PORTMUX_PORT_A, 1 << 31,
  154. PORTMUX_FUNC_A, drive_strength);
  155. break;
  156. case 2:
  157. portmux_select_peripheral(PORTMUX_PORT_B, 1 << 19,
  158. PORTMUX_FUNC_A, drive_strength);
  159. break;
  160. case 3:
  161. portmux_select_peripheral(PORTMUX_PORT_B, 1 << 29,
  162. PORTMUX_FUNC_A, drive_strength);
  163. break;
  164. case 4:
  165. portmux_select_peripheral(PORTMUX_PORT_B, 1 << 30,
  166. PORTMUX_FUNC_A, drive_strength);
  167. break;
  168. }
  169. }
  170. #endif /* __ASM_AVR32_ARCH_CLK_H__ */