Ilya Yanok | 1dc4da7 | 2009-06-08 04:12:45 +0400 | [diff] [blame] | 1 | /* |
| 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 Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 23 | #include <netdev.h> |
Ilya Yanok | 1dc4da7 | 2009-06-08 04:12:45 +0400 | [diff] [blame] | 24 | #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 | */ |
| 34 | unsigned 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 | |
| 47 | static ulong clk_in_32k(void) |
| 48 | { |
| 49 | return 1024 * CONFIG_MX27_CLK32; |
| 50 | } |
| 51 | |
| 52 | static 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 | |
| 64 | ulong 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 | |
| 78 | ulong 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 | |
| 93 | ulong 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 | |
| 105 | ulong 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 | |
| 119 | static ulong imx_decode_perclk(ulong div) |
| 120 | { |
| 121 | return lldiv((imx_get_mpllclk() * 2), (div * 3)); |
| 122 | } |
| 123 | |
| 124 | ulong 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 | |
| 131 | ulong 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 | |
| 138 | ulong 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 | |
| 145 | ulong 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) |
| 153 | int 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 Yanok | 0b23fb3 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 163 | int 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 Yanok | 1dc4da7 | 2009-06-08 04:12:45 +0400 | [diff] [blame] | 172 | void 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(®s->port[port].puen) | (1 << pin), |
| 185 | ®s->port[port].puen); |
| 186 | } else { |
| 187 | writel(readl(®s->port[port].puen) & ~(1 << pin), |
| 188 | ®s->port[port].puen); |
| 189 | } |
| 190 | |
| 191 | /* Data direction */ |
| 192 | if (gpio_mode & GPIO_OUT) { |
| 193 | writel(readl(®s->port[port].ddir) | 1 << pin, |
| 194 | ®s->port[port].ddir); |
| 195 | } else { |
| 196 | writel(readl(®s->port[port].ddir) & ~(1 << pin), |
| 197 | ®s->port[port].ddir); |
| 198 | } |
| 199 | |
| 200 | /* Primary / alternate function */ |
| 201 | if (gpio_mode & GPIO_AF) { |
| 202 | writel(readl(®s->port[port].gpr) | (1 << pin), |
| 203 | ®s->port[port].gpr); |
| 204 | } else { |
| 205 | writel(readl(®s->port[port].gpr) & ~(1 << pin), |
| 206 | ®s->port[port].gpr); |
| 207 | } |
| 208 | |
| 209 | /* use as gpio? */ |
| 210 | if (!(gpio_mode & (GPIO_PF | GPIO_AF))) { |
| 211 | writel(readl(®s->port[port].gius) | (1 << pin), |
| 212 | ®s->port[port].gius); |
| 213 | } else { |
| 214 | writel(readl(®s->port[port].gius) & ~(1 << pin), |
| 215 | ®s->port[port].gius); |
| 216 | } |
| 217 | |
| 218 | /* Output / input configuration */ |
| 219 | if (pin < 16) { |
| 220 | tmp = readl(®s->port[port].ocr1); |
| 221 | tmp &= ~(3 << (pin * 2)); |
| 222 | tmp |= (ocr << (pin * 2)); |
| 223 | writel(tmp, ®s->port[port].ocr1); |
| 224 | |
| 225 | writel(readl(®s->port[port].iconfa1) & ~(3 << (pin * 2)), |
| 226 | ®s->port[port].iconfa1); |
| 227 | writel(readl(®s->port[port].iconfa1) | aout << (pin * 2), |
| 228 | ®s->port[port].iconfa1); |
| 229 | writel(readl(®s->port[port].iconfb1) & ~(3 << (pin * 2)), |
| 230 | ®s->port[port].iconfb1); |
| 231 | writel(readl(®s->port[port].iconfb1) | bout << (pin * 2), |
| 232 | ®s->port[port].iconfb1); |
| 233 | } else { |
| 234 | pin -= 16; |
| 235 | |
| 236 | tmp = readl(®s->port[port].ocr2); |
| 237 | tmp &= ~(3 << (pin * 2)); |
| 238 | tmp |= (ocr << (pin * 2)); |
| 239 | writel(tmp, ®s->port[port].ocr2); |
| 240 | |
| 241 | writel(readl(®s->port[port].iconfa2) & ~(3 << (pin * 2)), |
| 242 | ®s->port[port].iconfa2); |
| 243 | writel(readl(®s->port[port].iconfa2) | aout << (pin * 2), |
| 244 | ®s->port[port].iconfa2); |
| 245 | writel(readl(®s->port[port].iconfb2) & ~(3 << (pin * 2)), |
| 246 | ®s->port[port].iconfb2); |
| 247 | writel(readl(®s->port[port].iconfb2) | bout << (pin * 2), |
| 248 | ®s->port[port].iconfb2); |
| 249 | } |
| 250 | } |