blob: 47fa4b48edd938ffdf3c74b4f605951ebf3203fb [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>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040023#include <netdev.h>
Ilya Yanok1dc4da72009-06-08 04:12:45 +040024#include <asm/io.h>
25#include <asm/arch/imx-regs.h>
26
27/*
28 * get the system pll clock in Hz
29 *
30 * mfi + mfn / (mfd +1)
31 * f = 2 * f_ref * --------------------
32 * pd + 1
33 */
34unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref)
35{
36 unsigned int mfi = (pll >> 10) & 0xf;
37 unsigned int mfn = pll & 0x3ff;
38 unsigned int mfd = (pll >> 16) & 0x3ff;
39 unsigned int pd = (pll >> 26) & 0xf;
40
41 mfi = mfi <= 5 ? 5 : mfi;
42
43 return lldiv(2 * (u64)f_ref * (mfi * (mfd + 1) + mfn),
44 (mfd + 1) * (pd + 1));
45}
46
47static ulong clk_in_32k(void)
48{
49 return 1024 * CONFIG_MX27_CLK32;
50}
51
52static ulong clk_in_26m(void)
53{
54 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
55
56 if (readl(&pll->cscr) & CSCR_OSC26M_DIV1P5) {
57 /* divide by 1.5 */
58 return 26000000 * 2 / 3;
59 } else {
60 return 26000000;
61 }
62}
63
64ulong imx_get_mpllclk(void)
65{
66 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
67 ulong cscr = readl(&pll->cscr);
68 ulong fref;
69
70 if (cscr & CSCR_MCU_SEL)
71 fref = clk_in_26m();
72 else
73 fref = clk_in_32k();
74
75 return imx_decode_pll(readl(&pll->mpctl0), fref);
76}
77
78ulong imx_get_armclk(void)
79{
80 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
81 ulong cscr = readl(&pll->cscr);
82 ulong fref = imx_get_mpllclk();
83 ulong div;
84
85 if (!(cscr & CSCR_ARM_SRC_MPLL))
86 fref = lldiv((fref * 2), 3);
87
88 div = ((cscr >> 12) & 0x3) + 1;
89
90 return lldiv(fref, div);
91}
92
93ulong imx_get_ahbclk(void)
94{
95 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
96 ulong cscr = readl(&pll->cscr);
97 ulong fref = imx_get_mpllclk();
98 ulong div;
99
100 div = ((cscr >> 8) & 0x3) + 1;
101
102 return lldiv(fref * 2, 3 * div);
103}
104
105ulong imx_get_spllclk(void)
106{
107 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
108 ulong cscr = readl(&pll->cscr);
109 ulong fref;
110
111 if (cscr & CSCR_SP_SEL)
112 fref = clk_in_26m();
113 else
114 fref = clk_in_32k();
115
116 return imx_decode_pll(readl(&pll->spctl0), fref);
117}
118
119static ulong imx_decode_perclk(ulong div)
120{
121 return lldiv((imx_get_mpllclk() * 2), (div * 3));
122}
123
124ulong imx_get_perclk1(void)
125{
126 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
127
128 return imx_decode_perclk((readl(&pll->pcdr1) & 0x3f) + 1);
129}
130
131ulong imx_get_perclk2(void)
132{
133 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
134
135 return imx_decode_perclk(((readl(&pll->pcdr1) >> 8) & 0x3f) + 1);
136}
137
138ulong imx_get_perclk3(void)
139{
140 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
141
142 return imx_decode_perclk(((readl(&pll->pcdr1) >> 16) & 0x3f) + 1);
143}
144
145ulong imx_get_perclk4(void)
146{
147 struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
148
149 return imx_decode_perclk(((readl(&pll->pcdr1) >> 24) & 0x3f) + 1);
150}
151
152#if defined(CONFIG_DISPLAY_CPUINFO)
153int print_cpuinfo (void)
154{
155 char buf[32];
156
157 printf("CPU: Freescale i.MX27 at %s MHz\n\n",
158 strmhz(buf, imx_get_mpllclk()));
159 return 0;
160}
161#endif
162
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400163int cpu_eth_init(bd_t *bis)
164{
165#if defined(CONFIG_FEC_MXC)
166 return fecmxc_initialize(bis);
167#else
168 return 0;
169#endif
170}
171
Ilya Yanok1dc4da72009-06-08 04:12:45 +0400172void imx_gpio_mode(int gpio_mode)
173{
174 struct gpio_regs *regs = (struct gpio_regs *)IMX_GPIO_BASE;
175 unsigned int pin = gpio_mode & GPIO_PIN_MASK;
176 unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
177 unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
178 unsigned int aout = (gpio_mode & GPIO_AOUT_MASK) >> GPIO_AOUT_SHIFT;
179 unsigned int bout = (gpio_mode & GPIO_BOUT_MASK) >> GPIO_BOUT_SHIFT;
180 unsigned int tmp;
181
182 /* Pullup enable */
183 if (gpio_mode & GPIO_PUEN) {
184 writel(readl(&regs->port[port].puen) | (1 << pin),
185 &regs->port[port].puen);
186 } else {
187 writel(readl(&regs->port[port].puen) & ~(1 << pin),
188 &regs->port[port].puen);
189 }
190
191 /* Data direction */
192 if (gpio_mode & GPIO_OUT) {
193 writel(readl(&regs->port[port].ddir) | 1 << pin,
194 &regs->port[port].ddir);
195 } else {
196 writel(readl(&regs->port[port].ddir) & ~(1 << pin),
197 &regs->port[port].ddir);
198 }
199
200 /* Primary / alternate function */
201 if (gpio_mode & GPIO_AF) {
202 writel(readl(&regs->port[port].gpr) | (1 << pin),
203 &regs->port[port].gpr);
204 } else {
205 writel(readl(&regs->port[port].gpr) & ~(1 << pin),
206 &regs->port[port].gpr);
207 }
208
209 /* use as gpio? */
210 if (!(gpio_mode & (GPIO_PF | GPIO_AF))) {
211 writel(readl(&regs->port[port].gius) | (1 << pin),
212 &regs->port[port].gius);
213 } else {
214 writel(readl(&regs->port[port].gius) & ~(1 << pin),
215 &regs->port[port].gius);
216 }
217
218 /* Output / input configuration */
219 if (pin < 16) {
220 tmp = readl(&regs->port[port].ocr1);
221 tmp &= ~(3 << (pin * 2));
222 tmp |= (ocr << (pin * 2));
223 writel(tmp, &regs->port[port].ocr1);
224
225 writel(readl(&regs->port[port].iconfa1) & ~(3 << (pin * 2)),
226 &regs->port[port].iconfa1);
227 writel(readl(&regs->port[port].iconfa1) | aout << (pin * 2),
228 &regs->port[port].iconfa1);
229 writel(readl(&regs->port[port].iconfb1) & ~(3 << (pin * 2)),
230 &regs->port[port].iconfb1);
231 writel(readl(&regs->port[port].iconfb1) | bout << (pin * 2),
232 &regs->port[port].iconfb1);
233 } else {
234 pin -= 16;
235
236 tmp = readl(&regs->port[port].ocr2);
237 tmp &= ~(3 << (pin * 2));
238 tmp |= (ocr << (pin * 2));
239 writel(tmp, &regs->port[port].ocr2);
240
241 writel(readl(&regs->port[port].iconfa2) & ~(3 << (pin * 2)),
242 &regs->port[port].iconfa2);
243 writel(readl(&regs->port[port].iconfa2) | aout << (pin * 2),
244 &regs->port[port].iconfa2);
245 writel(readl(&regs->port[port].iconfb2) & ~(3 << (pin * 2)),
246 &regs->port[port].iconfb2);
247 writel(readl(&regs->port[port].iconfb2) | bout << (pin * 2),
248 &regs->port[port].iconfb2);
249 }
250}