blob: 74cf27d48bfbb286566212d6383d39e7e13ae20c [file] [log] [blame]
Sascha Hauer8c25c362009-06-04 11:32:12 +02001/*
2 * Copyright (C) 2009 by Sascha Hauer, Pengutronix
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 * MA 02110-1301, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/list.h>
22#include <linux/clk.h>
23#include <linux/io.h>
24
25#include <asm/clkdev.h>
26
27#include <mach/clock.h>
28#include <mach/hardware.h>
29#include <mach/common.h>
30#include <mach/mx25.h>
31
32#define CRM_BASE MX25_IO_ADDRESS(MX25_CRM_BASE_ADDR)
33
34#define CCM_MPCTL 0x00
35#define CCM_UPCTL 0x04
36#define CCM_CCTL 0x08
37#define CCM_CGCR0 0x0C
38#define CCM_CGCR1 0x10
39#define CCM_CGCR2 0x14
40#define CCM_PCDR0 0x18
41#define CCM_PCDR1 0x1C
42#define CCM_PCDR2 0x20
43#define CCM_PCDR3 0x24
44#define CCM_RCSR 0x28
45#define CCM_CRDR 0x2C
46#define CCM_DCVR0 0x30
47#define CCM_DCVR1 0x34
48#define CCM_DCVR2 0x38
49#define CCM_DCVR3 0x3c
50#define CCM_LTR0 0x40
51#define CCM_LTR1 0x44
52#define CCM_LTR2 0x48
53#define CCM_LTR3 0x4c
54
55static unsigned long get_rate_mpll(void)
56{
57 ulong mpctl = __raw_readl(CRM_BASE + CCM_MPCTL);
58
59 return mxc_decode_pll(mpctl, 24000000);
60}
61
62static unsigned long get_rate_upll(void)
63{
64 ulong mpctl = __raw_readl(CRM_BASE + CCM_UPCTL);
65
66 return mxc_decode_pll(mpctl, 24000000);
67}
68
69unsigned long get_rate_arm(struct clk *clk)
70{
71 unsigned long cctl = readl(CRM_BASE + CCM_CCTL);
72 unsigned long rate = get_rate_mpll();
73
74 if (cctl & (1 << 14))
75 rate = (rate * 3) >> 1;
76
77 return rate / ((cctl >> 30) + 1);
78}
79
80static unsigned long get_rate_ahb(struct clk *clk)
81{
82 unsigned long cctl = readl(CRM_BASE + CCM_CCTL);
83
84 return get_rate_arm(NULL) / (((cctl >> 28) & 0x3) + 1);
85}
86
87static unsigned long get_rate_ipg(struct clk *clk)
88{
89 return get_rate_ahb(NULL) >> 1;
90}
91
92static unsigned long get_rate_per(int per)
93{
94 unsigned long ofs = (per & 0x3) * 8;
95 unsigned long reg = per & ~0x3;
96 unsigned long val = (readl(CRM_BASE + CCM_PCDR0 + reg) >> ofs) & 0x3f;
97 unsigned long fref;
98
99 if (readl(CRM_BASE + 0x64) & (1 << per))
100 fref = get_rate_upll();
101 else
102 fref = get_rate_ipg(NULL);
103
104 return fref / (val + 1);
105}
106
107static unsigned long get_rate_uart(struct clk *clk)
108{
109 return get_rate_per(15);
110}
111
Eric Bénard8402ed32010-06-08 11:03:00 +0200112static unsigned long get_rate_ssi2(struct clk *clk)
113{
114 return get_rate_per(14);
115}
116
117static unsigned long get_rate_ssi1(struct clk *clk)
118{
119 return get_rate_per(13);
120}
121
Sascha Hauer8c25c362009-06-04 11:32:12 +0200122static unsigned long get_rate_i2c(struct clk *clk)
123{
124 return get_rate_per(6);
125}
126
127static unsigned long get_rate_nfc(struct clk *clk)
128{
129 return get_rate_per(8);
130}
131
Baruch Siachfaed4062010-01-25 12:58:21 +0200132static unsigned long get_rate_gpt(struct clk *clk)
133{
134 return get_rate_per(5);
135}
136
Baruch Siach04a03e52010-02-17 12:33:24 +0200137static unsigned long get_rate_lcdc(struct clk *clk)
138{
139 return get_rate_per(7);
140}
141
Sascha Hauer8c25c362009-06-04 11:32:12 +0200142static unsigned long get_rate_otg(struct clk *clk)
143{
Eric Bénarda6e92b42010-06-08 11:02:57 +0200144 unsigned long cctl = readl(CRM_BASE + CCM_CCTL);
145 unsigned long rate = get_rate_upll();
146
147 return (cctl & (1 << 23)) ? 0 : rate / ((0x3F & (cctl >> 16)) + 1);
Sascha Hauer8c25c362009-06-04 11:32:12 +0200148}
149
150static int clk_cgcr_enable(struct clk *clk)
151{
152 u32 reg;
153
154 reg = __raw_readl(clk->enable_reg);
155 reg |= 1 << clk->enable_shift;
156 __raw_writel(reg, clk->enable_reg);
157
158 return 0;
159}
160
161static void clk_cgcr_disable(struct clk *clk)
162{
163 u32 reg;
164
165 reg = __raw_readl(clk->enable_reg);
166 reg &= ~(1 << clk->enable_shift);
167 __raw_writel(reg, clk->enable_reg);
168}
169
Sascha Hauer9611a9b2010-01-22 08:46:13 +0100170#define DEFINE_CLOCK(name, i, er, es, gr, sr, s) \
Sascha Hauer8c25c362009-06-04 11:32:12 +0200171 static struct clk name = { \
172 .id = i, \
173 .enable_reg = CRM_BASE + er, \
174 .enable_shift = es, \
175 .get_rate = gr, \
176 .set_rate = sr, \
177 .enable = clk_cgcr_enable, \
178 .disable = clk_cgcr_disable, \
Sascha Hauer9611a9b2010-01-22 08:46:13 +0100179 .secondary = s, \
Sascha Hauer8c25c362009-06-04 11:32:12 +0200180 }
181
Baruch Siachfaed4062010-01-25 12:58:21 +0200182DEFINE_CLOCK(gpt_clk, 0, CCM_CGCR0, 5, get_rate_gpt, NULL, NULL);
Sascha Hauer4cd3f962010-01-22 08:47:06 +0100183DEFINE_CLOCK(uart_per_clk, 0, CCM_CGCR0, 15, get_rate_uart, NULL, NULL);
Eric Bénard8402ed32010-06-08 11:03:00 +0200184DEFINE_CLOCK(ssi1_per_clk, 0, CCM_CGCR0, 13, get_rate_ipg, NULL, NULL);
185DEFINE_CLOCK(ssi2_per_clk, 0, CCM_CGCR0, 14, get_rate_ipg, NULL, NULL);
Sascha Hauer9611a9b2010-01-22 08:46:13 +0100186DEFINE_CLOCK(cspi1_clk, 0, CCM_CGCR1, 5, get_rate_ipg, NULL, NULL);
187DEFINE_CLOCK(cspi2_clk, 0, CCM_CGCR1, 6, get_rate_ipg, NULL, NULL);
188DEFINE_CLOCK(cspi3_clk, 0, CCM_CGCR1, 7, get_rate_ipg, NULL, NULL);
Baruch Siach1c574022010-01-25 12:58:22 +0200189DEFINE_CLOCK(fec_ahb_clk, 0, CCM_CGCR0, 23, NULL, NULL, NULL);
Baruch Siach04a03e52010-02-17 12:33:24 +0200190DEFINE_CLOCK(lcdc_ahb_clk, 0, CCM_CGCR0, 24, NULL, NULL, NULL);
191DEFINE_CLOCK(lcdc_per_clk, 0, CCM_CGCR0, 7, NULL, NULL, &lcdc_ahb_clk);
Sascha Hauer4cd3f962010-01-22 08:47:06 +0100192DEFINE_CLOCK(uart1_clk, 0, CCM_CGCR2, 14, get_rate_uart, NULL, &uart_per_clk);
193DEFINE_CLOCK(uart2_clk, 0, CCM_CGCR2, 15, get_rate_uart, NULL, &uart_per_clk);
194DEFINE_CLOCK(uart3_clk, 0, CCM_CGCR2, 16, get_rate_uart, NULL, &uart_per_clk);
195DEFINE_CLOCK(uart4_clk, 0, CCM_CGCR2, 17, get_rate_uart, NULL, &uart_per_clk);
196DEFINE_CLOCK(uart5_clk, 0, CCM_CGCR2, 18, get_rate_uart, NULL, &uart_per_clk);
Sascha Hauer9611a9b2010-01-22 08:46:13 +0100197DEFINE_CLOCK(nfc_clk, 0, CCM_CGCR0, 8, get_rate_nfc, NULL, NULL);
198DEFINE_CLOCK(usbotg_clk, 0, CCM_CGCR0, 28, get_rate_otg, NULL, NULL);
199DEFINE_CLOCK(pwm1_clk, 0, CCM_CGCR1, 31, get_rate_ipg, NULL, NULL);
200DEFINE_CLOCK(pwm2_clk, 0, CCM_CGCR2, 0, get_rate_ipg, NULL, NULL);
201DEFINE_CLOCK(pwm3_clk, 0, CCM_CGCR2, 1, get_rate_ipg, NULL, NULL);
202DEFINE_CLOCK(pwm4_clk, 0, CCM_CGCR2, 2, get_rate_ipg, NULL, NULL);
203DEFINE_CLOCK(kpp_clk, 0, CCM_CGCR1, 28, get_rate_ipg, NULL, NULL);
204DEFINE_CLOCK(tsc_clk, 0, CCM_CGCR2, 13, get_rate_ipg, NULL, NULL);
205DEFINE_CLOCK(i2c_clk, 0, CCM_CGCR0, 6, get_rate_i2c, NULL, NULL);
Baruch Siach1c574022010-01-25 12:58:22 +0200206DEFINE_CLOCK(fec_clk, 0, CCM_CGCR1, 15, get_rate_ipg, NULL, &fec_ahb_clk);
Baruch Siachdcbabbc2010-01-27 15:00:48 +0200207DEFINE_CLOCK(dryice_clk, 0, CCM_CGCR1, 8, get_rate_ipg, NULL, NULL);
Baruch Siach04a03e52010-02-17 12:33:24 +0200208DEFINE_CLOCK(lcdc_clk, 0, CCM_CGCR1, 29, get_rate_lcdc, NULL, &lcdc_per_clk);
Baruch Siach30816ab2010-06-10 12:32:15 +0300209DEFINE_CLOCK(wdt_clk, 0, CCM_CGCR2, 19, get_rate_ipg, NULL, NULL);
Eric Bénard8402ed32010-06-08 11:03:00 +0200210DEFINE_CLOCK(ssi1_clk, 0, CCM_CGCR2, 11, get_rate_ssi1, NULL, &ssi1_per_clk);
211DEFINE_CLOCK(ssi2_clk, 1, CCM_CGCR2, 12, get_rate_ssi2, NULL, &ssi2_per_clk);
212DEFINE_CLOCK(audmux_clk, 0, CCM_CGCR1, 0, NULL, NULL, NULL);
Sascha Hauer8c25c362009-06-04 11:32:12 +0200213
214#define _REGISTER_CLOCK(d, n, c) \
215 { \
216 .dev_id = d, \
217 .con_id = n, \
218 .clk = &c, \
219 },
220
221static struct clk_lookup lookups[] = {
222 _REGISTER_CLOCK("imx-uart.0", NULL, uart1_clk)
223 _REGISTER_CLOCK("imx-uart.1", NULL, uart2_clk)
224 _REGISTER_CLOCK("imx-uart.2", NULL, uart3_clk)
225 _REGISTER_CLOCK("imx-uart.3", NULL, uart4_clk)
226 _REGISTER_CLOCK("imx-uart.4", NULL, uart5_clk)
227 _REGISTER_CLOCK("mxc-ehci.0", "usb", usbotg_clk)
228 _REGISTER_CLOCK("mxc-ehci.1", "usb", usbotg_clk)
229 _REGISTER_CLOCK("mxc-ehci.2", "usb", usbotg_clk)
230 _REGISTER_CLOCK("fsl-usb2-udc", "usb", usbotg_clk)
231 _REGISTER_CLOCK("mxc_nand.0", NULL, nfc_clk)
232 _REGISTER_CLOCK("spi_imx.0", NULL, cspi1_clk)
233 _REGISTER_CLOCK("spi_imx.1", NULL, cspi2_clk)
234 _REGISTER_CLOCK("spi_imx.2", NULL, cspi3_clk)
235 _REGISTER_CLOCK("mxc_pwm.0", NULL, pwm1_clk)
236 _REGISTER_CLOCK("mxc_pwm.1", NULL, pwm2_clk)
237 _REGISTER_CLOCK("mxc_pwm.2", NULL, pwm3_clk)
238 _REGISTER_CLOCK("mxc_pwm.3", NULL, pwm4_clk)
Baruch Siach49535a92010-05-26 15:12:10 +0300239 _REGISTER_CLOCK("imx-keypad", NULL, kpp_clk)
Sascha Hauer8c25c362009-06-04 11:32:12 +0200240 _REGISTER_CLOCK("mx25-adc", NULL, tsc_clk)
241 _REGISTER_CLOCK("imx-i2c.0", NULL, i2c_clk)
242 _REGISTER_CLOCK("imx-i2c.1", NULL, i2c_clk)
243 _REGISTER_CLOCK("imx-i2c.2", NULL, i2c_clk)
Baruch Siacha7595442009-12-21 13:44:31 +0200244 _REGISTER_CLOCK("fec.0", NULL, fec_clk)
Baruch Siachdcbabbc2010-01-27 15:00:48 +0200245 _REGISTER_CLOCK("imxdi_rtc.0", NULL, dryice_clk)
Baruch Siach04a03e52010-02-17 12:33:24 +0200246 _REGISTER_CLOCK("imx-fb.0", NULL, lcdc_clk)
Baruch Siach30816ab2010-06-10 12:32:15 +0300247 _REGISTER_CLOCK("imx-wdt.0", NULL, wdt_clk)
Eric Bénard8402ed32010-06-08 11:03:00 +0200248 _REGISTER_CLOCK("imx-ssi.0", NULL, ssi1_clk)
249 _REGISTER_CLOCK("imx-ssi.1", NULL, ssi2_clk)
250 _REGISTER_CLOCK(NULL, "audmux", audmux_clk)
Sascha Hauer8c25c362009-06-04 11:32:12 +0200251};
252
Baruch Siachfadc0952010-01-25 12:58:19 +0200253int __init mx25_clocks_init(void)
Sascha Hauer8c25c362009-06-04 11:32:12 +0200254{
Russell King0a0300d2010-01-12 12:28:00 +0000255 clkdev_add_table(lookups, ARRAY_SIZE(lookups));
Sascha Hauer8c25c362009-06-04 11:32:12 +0200256
Baruch Siach828df432010-01-25 12:58:20 +0200257 /* Turn off all clocks except the ones we need to survive, namely:
258 * EMI, GPIO1-3 (CCM_CGCR1[18:16]), GPT1, IOMUXC (CCM_CGCR1[27]), IIM,
259 * SCC
260 */
261 __raw_writel((1 << 19), CRM_BASE + CCM_CGCR0);
262 __raw_writel((0xf << 16) | (3 << 26), CRM_BASE + CCM_CGCR1);
263 __raw_writel((1 << 5), CRM_BASE + CCM_CGCR2);
264
Baruch Siach04a03e52010-02-17 12:33:24 +0200265 /* Clock source for lcdc is upll */
266 __raw_writel(__raw_readl(CRM_BASE+0x64) | (1 << 7), CRM_BASE + 0x64);
267
Sascha Hauer8c25c362009-06-04 11:32:12 +0200268 mxc_timer_init(&gpt_clk, MX25_IO_ADDRESS(MX25_GPT1_BASE_ADDR), 54);
269
270 return 0;
271}