blob: eab54d86df4bed2726ea44e4bb0b4dc3461bdcaf [file] [log] [blame]
Ilya Yanok1dc4da72009-06-08 04:12:45 +04001/*
2 * Copyright (c) 2008 Eric Jarrige <eric.jarrige@armadeus.org>
3 * Copyright (c) 2009 Ilya Yanok <yanok@emcraft.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 */
20
21#include <common.h>
22#include <div64.h>
23#include <asm/io.h>
24#include <asm/arch/imx-regs.h>
25
26/*
27 * get the system pll clock in Hz
28 *
29 * mfi + mfn / (mfd +1)
30 * f = 2 * f_ref * --------------------
31 * pd + 1
32 */
33unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
34{
35 unsigned int mfi = (pll >> 10) & 0xf;
36 unsigned int mfn = pll & 0x3ff;
37 unsigned int mfd = (pll >> 16) & 0x3ff;
38 unsigned int pd = (pll >> 26) & 0xf;
39
40 mfi = mfi <= 5 ? 5 : mfi;
41
42 return lldiv(2 * (u64)f_ref * (mfi * (mfd + 1) + mfn),
43 (mfd + 1) * (pd + 1));
44}
45
46static ulong clk_in_32k(void)
47{
48 return 1024 * CONFIG_MX27_CLK32;
49}
50
51static ulong clk_in_26m(void)
52{
53 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
54
55 if (readl(&pll->cscr) & CSCR_OSC26M_DIV1P5) {
56 /* divide by 1.5 */
57 return 26000000 * 2 / 3;
58 } else {
59 return 26000000;
60 }
61}
62
63ulong imx_get_mpllclk(void)
64{
65 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
66 ulong cscr = readl(&pll->cscr);
67 ulong fref;
68
69 if (cscr & CSCR_MCU_SEL)
70 fref = clk_in_26m();
71 else
72 fref = clk_in_32k();
73
74 return imx_decode_pll(readl(&pll->mpctl0), fref);
75}
76
77ulong imx_get_armclk(void)
78{
79 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
80 ulong cscr = readl(&pll->cscr);
81 ulong fref = imx_get_mpllclk();
82 ulong div;
83
84 if (!(cscr & CSCR_ARM_SRC_MPLL))
85 fref = lldiv((fref * 2), 3);
86
87 div = ((cscr >> 12) & 0x3) + 1;
88
89 return lldiv(fref, div);
90}
91
92ulong imx_get_ahbclk(void)
93{
94 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
95 ulong cscr = readl(&pll->cscr);
96 ulong fref = imx_get_mpllclk();
97 ulong div;
98
99 div = ((cscr >> 8) & 0x3) + 1;
100
101 return lldiv(fref * 2, 3 * div);
102}
103
104ulong imx_get_spllclk(void)
105{
106 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
107 ulong cscr = readl(&pll->cscr);
108 ulong fref;
109
110 if (cscr & CSCR_SP_SEL)
111 fref = clk_in_26m();
112 else
113 fref = clk_in_32k();
114
115 return imx_decode_pll(readl(&pll->spctl0), fref);
116}
117
118static ulong imx_decode_perclk(ulong div)
119{
120 return lldiv((imx_get_mpllclk() * 2), (div * 3));
121}
122
123ulong imx_get_perclk1(void)
124{
125 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
126
127 return imx_decode_perclk((readl(&pll->pcdr1) & 0x3f) + 1);
128}
129
130ulong imx_get_perclk2(void)
131{
132 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
133
134 return imx_decode_perclk(((readl(&pll->pcdr1) >> 8) & 0x3f) + 1);
135}
136
137ulong imx_get_perclk3(void)
138{
139 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
140
141 return imx_decode_perclk(((readl(&pll->pcdr1) >> 16) & 0x3f) + 1);
142}
143
144ulong imx_get_perclk4(void)
145{
146 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
147
148 return imx_decode_perclk(((readl(&pll->pcdr1) >> 24) & 0x3f) + 1);
149}
150
151#if defined(CONFIG_DISPLAY_CPUINFO)
152int print_cpuinfo (void)
153{
154 char buf[32];
155
156 printf("CPU: Freescale i.MX27 at %s MHz\n\n",
157 strmhz(buf, imx_get_mpllclk()));
158 return 0;
159}
160#endif
161
162void imx_gpio_mode(int gpio_mode)
163{
164 struct gpio_regs *regs = (struct gpio_regs *)IMX_GPIO_BASE;
165 unsigned int pin = gpio_mode & GPIO_PIN_MASK;
166 unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
167 unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
168 unsigned int aout = (gpio_mode & GPIO_AOUT_MASK) >> GPIO_AOUT_SHIFT;
169 unsigned int bout = (gpio_mode & GPIO_BOUT_MASK) >> GPIO_BOUT_SHIFT;
170 unsigned int tmp;
171
172 /* Pullup enable */
173 if (gpio_mode & GPIO_PUEN) {
174 writel(readl(&regs->port[port].puen) | (1 << pin),
175 &regs->port[port].puen);
176 } else {
177 writel(readl(&regs->port[port].puen) & ~(1 << pin),
178 &regs->port[port].puen);
179 }
180
181 /* Data direction */
182 if (gpio_mode & GPIO_OUT) {
183 writel(readl(&regs->port[port].ddir) | 1 << pin,
184 &regs->port[port].ddir);
185 } else {
186 writel(readl(&regs->port[port].ddir) & ~(1 << pin),
187 &regs->port[port].ddir);
188 }
189
190 /* Primary / alternate function */
191 if (gpio_mode & GPIO_AF) {
192 writel(readl(&regs->port[port].gpr) | (1 << pin),
193 &regs->port[port].gpr);
194 } else {
195 writel(readl(&regs->port[port].gpr) & ~(1 << pin),
196 &regs->port[port].gpr);
197 }
198
199 /* use as gpio? */
200 if (!(gpio_mode & (GPIO_PF | GPIO_AF))) {
201 writel(readl(&regs->port[port].gius) | (1 << pin),
202 &regs->port[port].gius);
203 } else {
204 writel(readl(&regs->port[port].gius) & ~(1 << pin),
205 &regs->port[port].gius);
206 }
207
208 /* Output / input configuration */
209 if (pin < 16) {
210 tmp = readl(&regs->port[port].ocr1);
211 tmp &= ~(3 << (pin * 2));
212 tmp |= (ocr << (pin * 2));
213 writel(tmp, &regs->port[port].ocr1);
214
215 writel(readl(&regs->port[port].iconfa1) & ~(3 << (pin * 2)),
216 &regs->port[port].iconfa1);
217 writel(readl(&regs->port[port].iconfa1) | aout << (pin * 2),
218 &regs->port[port].iconfa1);
219 writel(readl(&regs->port[port].iconfb1) & ~(3 << (pin * 2)),
220 &regs->port[port].iconfb1);
221 writel(readl(&regs->port[port].iconfb1) | bout << (pin * 2),
222 &regs->port[port].iconfb1);
223 } else {
224 pin -= 16;
225
226 tmp = readl(&regs->port[port].ocr2);
227 tmp &= ~(3 << (pin * 2));
228 tmp |= (ocr << (pin * 2));
229 writel(tmp, &regs->port[port].ocr2);
230
231 writel(readl(&regs->port[port].iconfa2) & ~(3 << (pin * 2)),
232 &regs->port[port].iconfa2);
233 writel(readl(&regs->port[port].iconfa2) | aout << (pin * 2),
234 &regs->port[port].iconfa2);
235 writel(readl(&regs->port[port].iconfb2) & ~(3 << (pin * 2)),
236 &regs->port[port].iconfb2);
237 writel(readl(&regs->port[port].iconfb2) | bout << (pin * 2),
238 &regs->port[port].iconfb2);
239 }
240}
241