Ilya Yanok | 416a41f | 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 | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 23 | #include <netdev.h> |
Ilya Yanok | 416a41f | 2009-06-08 04:12:45 +0400 | [diff] [blame] | 24 | #include <asm/io.h> |
| 25 | #include <asm/arch/imx-regs.h> |
Ilya Yanok | 1ed4ae0 | 2009-06-08 04:12:49 +0400 | [diff] [blame] | 26 | #ifdef CONFIG_MXC_MMC |
| 27 | #include <asm/arch/mxcmmc.h> |
| 28 | #endif |
Ilya Yanok | 416a41f | 2009-06-08 04:12:45 +0400 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * get the system pll clock in Hz |
| 32 | * |
| 33 | * mfi + mfn / (mfd +1) |
| 34 | * f = 2 * f_ref * -------------------- |
| 35 | * pd + 1 |
| 36 | */ |
| 37 | unsigned int imx_decode_pll(unsigned int pll, unsigned int f_ref) |
| 38 | { |
| 39 | unsigned int mfi = (pll >> 10) & 0xf; |
| 40 | unsigned int mfn = pll & 0x3ff; |
| 41 | unsigned int mfd = (pll >> 16) & 0x3ff; |
| 42 | unsigned int pd = (pll >> 26) & 0xf; |
| 43 | |
| 44 | mfi = mfi <= 5 ? 5 : mfi; |
| 45 | |
| 46 | return lldiv(2 * (u64)f_ref * (mfi * (mfd + 1) + mfn), |
| 47 | (mfd + 1) * (pd + 1)); |
| 48 | } |
| 49 | |
| 50 | static ulong clk_in_32k(void) |
| 51 | { |
| 52 | return 1024 * CONFIG_MX27_CLK32; |
| 53 | } |
| 54 | |
| 55 | static ulong clk_in_26m(void) |
| 56 | { |
| 57 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 58 | |
| 59 | if (readl(&pll->cscr) & CSCR_OSC26M_DIV1P5) { |
| 60 | /* divide by 1.5 */ |
| 61 | return 26000000 * 2 / 3; |
| 62 | } else { |
| 63 | return 26000000; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | ulong imx_get_mpllclk(void) |
| 68 | { |
| 69 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 70 | ulong cscr = readl(&pll->cscr); |
| 71 | ulong fref; |
| 72 | |
| 73 | if (cscr & CSCR_MCU_SEL) |
| 74 | fref = clk_in_26m(); |
| 75 | else |
| 76 | fref = clk_in_32k(); |
| 77 | |
| 78 | return imx_decode_pll(readl(&pll->mpctl0), fref); |
| 79 | } |
| 80 | |
| 81 | ulong imx_get_armclk(void) |
| 82 | { |
| 83 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 84 | ulong cscr = readl(&pll->cscr); |
| 85 | ulong fref = imx_get_mpllclk(); |
| 86 | ulong div; |
| 87 | |
| 88 | if (!(cscr & CSCR_ARM_SRC_MPLL)) |
| 89 | fref = lldiv((fref * 2), 3); |
| 90 | |
| 91 | div = ((cscr >> 12) & 0x3) + 1; |
| 92 | |
| 93 | return lldiv(fref, div); |
| 94 | } |
| 95 | |
| 96 | ulong imx_get_ahbclk(void) |
| 97 | { |
| 98 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 99 | ulong cscr = readl(&pll->cscr); |
| 100 | ulong fref = imx_get_mpllclk(); |
| 101 | ulong div; |
| 102 | |
| 103 | div = ((cscr >> 8) & 0x3) + 1; |
| 104 | |
| 105 | return lldiv(fref * 2, 3 * div); |
| 106 | } |
| 107 | |
| 108 | ulong imx_get_spllclk(void) |
| 109 | { |
| 110 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 111 | ulong cscr = readl(&pll->cscr); |
| 112 | ulong fref; |
| 113 | |
| 114 | if (cscr & CSCR_SP_SEL) |
| 115 | fref = clk_in_26m(); |
| 116 | else |
| 117 | fref = clk_in_32k(); |
| 118 | |
| 119 | return imx_decode_pll(readl(&pll->spctl0), fref); |
| 120 | } |
| 121 | |
| 122 | static ulong imx_decode_perclk(ulong div) |
| 123 | { |
| 124 | return lldiv((imx_get_mpllclk() * 2), (div * 3)); |
| 125 | } |
| 126 | |
| 127 | ulong imx_get_perclk1(void) |
| 128 | { |
| 129 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 130 | |
| 131 | return imx_decode_perclk((readl(&pll->pcdr1) & 0x3f) + 1); |
| 132 | } |
| 133 | |
| 134 | ulong imx_get_perclk2(void) |
| 135 | { |
| 136 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 137 | |
| 138 | return imx_decode_perclk(((readl(&pll->pcdr1) >> 8) & 0x3f) + 1); |
| 139 | } |
| 140 | |
| 141 | ulong imx_get_perclk3(void) |
| 142 | { |
| 143 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 144 | |
| 145 | return imx_decode_perclk(((readl(&pll->pcdr1) >> 16) & 0x3f) + 1); |
| 146 | } |
| 147 | |
| 148 | ulong imx_get_perclk4(void) |
| 149 | { |
| 150 | struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE; |
| 151 | |
| 152 | return imx_decode_perclk(((readl(&pll->pcdr1) >> 24) & 0x3f) + 1); |
| 153 | } |
| 154 | |
| 155 | #if defined(CONFIG_DISPLAY_CPUINFO) |
| 156 | int print_cpuinfo (void) |
| 157 | { |
| 158 | char buf[32]; |
| 159 | |
| 160 | printf("CPU: Freescale i.MX27 at %s MHz\n\n", |
| 161 | strmhz(buf, imx_get_mpllclk())); |
| 162 | return 0; |
| 163 | } |
| 164 | #endif |
| 165 | |
Ilya Yanok | e93a4a5 | 2009-07-21 19:32:21 +0400 | [diff] [blame] | 166 | int cpu_eth_init(bd_t *bis) |
| 167 | { |
| 168 | #if defined(CONFIG_FEC_MXC) |
| 169 | return fecmxc_initialize(bis); |
| 170 | #else |
| 171 | return 0; |
| 172 | #endif |
| 173 | } |
| 174 | |
Ilya Yanok | 1ed4ae0 | 2009-06-08 04:12:49 +0400 | [diff] [blame] | 175 | /* |
| 176 | * Initializes on-chip MMC controllers. |
| 177 | * to override, implement board_mmc_init() |
| 178 | */ |
| 179 | int cpu_mmc_init(bd_t *bis) |
| 180 | { |
| 181 | #ifdef CONFIG_MXC_MMC |
| 182 | return mxc_mmc_init(bis); |
| 183 | #else |
| 184 | return 0; |
| 185 | #endif |
| 186 | } |
| 187 | |
Ilya Yanok | 416a41f | 2009-06-08 04:12:45 +0400 | [diff] [blame] | 188 | void imx_gpio_mode(int gpio_mode) |
| 189 | { |
| 190 | struct gpio_regs *regs = (struct gpio_regs *)IMX_GPIO_BASE; |
| 191 | unsigned int pin = gpio_mode & GPIO_PIN_MASK; |
| 192 | unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT; |
| 193 | unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT; |
| 194 | unsigned int aout = (gpio_mode & GPIO_AOUT_MASK) >> GPIO_AOUT_SHIFT; |
| 195 | unsigned int bout = (gpio_mode & GPIO_BOUT_MASK) >> GPIO_BOUT_SHIFT; |
| 196 | unsigned int tmp; |
| 197 | |
| 198 | /* Pullup enable */ |
| 199 | if (gpio_mode & GPIO_PUEN) { |
| 200 | writel(readl(®s->port[port].puen) | (1 << pin), |
| 201 | ®s->port[port].puen); |
| 202 | } else { |
| 203 | writel(readl(®s->port[port].puen) & ~(1 << pin), |
| 204 | ®s->port[port].puen); |
| 205 | } |
| 206 | |
| 207 | /* Data direction */ |
| 208 | if (gpio_mode & GPIO_OUT) { |
| 209 | writel(readl(®s->port[port].ddir) | 1 << pin, |
| 210 | ®s->port[port].ddir); |
| 211 | } else { |
| 212 | writel(readl(®s->port[port].ddir) & ~(1 << pin), |
| 213 | ®s->port[port].ddir); |
| 214 | } |
| 215 | |
| 216 | /* Primary / alternate function */ |
| 217 | if (gpio_mode & GPIO_AF) { |
| 218 | writel(readl(®s->port[port].gpr) | (1 << pin), |
| 219 | ®s->port[port].gpr); |
| 220 | } else { |
| 221 | writel(readl(®s->port[port].gpr) & ~(1 << pin), |
| 222 | ®s->port[port].gpr); |
| 223 | } |
| 224 | |
| 225 | /* use as gpio? */ |
| 226 | if (!(gpio_mode & (GPIO_PF | GPIO_AF))) { |
| 227 | writel(readl(®s->port[port].gius) | (1 << pin), |
| 228 | ®s->port[port].gius); |
| 229 | } else { |
| 230 | writel(readl(®s->port[port].gius) & ~(1 << pin), |
| 231 | ®s->port[port].gius); |
| 232 | } |
| 233 | |
| 234 | /* Output / input configuration */ |
| 235 | if (pin < 16) { |
| 236 | tmp = readl(®s->port[port].ocr1); |
| 237 | tmp &= ~(3 << (pin * 2)); |
| 238 | tmp |= (ocr << (pin * 2)); |
| 239 | writel(tmp, ®s->port[port].ocr1); |
| 240 | |
| 241 | writel(readl(®s->port[port].iconfa1) & ~(3 << (pin * 2)), |
| 242 | ®s->port[port].iconfa1); |
| 243 | writel(readl(®s->port[port].iconfa1) | aout << (pin * 2), |
| 244 | ®s->port[port].iconfa1); |
| 245 | writel(readl(®s->port[port].iconfb1) & ~(3 << (pin * 2)), |
| 246 | ®s->port[port].iconfb1); |
| 247 | writel(readl(®s->port[port].iconfb1) | bout << (pin * 2), |
| 248 | ®s->port[port].iconfb1); |
| 249 | } else { |
| 250 | pin -= 16; |
| 251 | |
| 252 | tmp = readl(®s->port[port].ocr2); |
| 253 | tmp &= ~(3 << (pin * 2)); |
| 254 | tmp |= (ocr << (pin * 2)); |
| 255 | writel(tmp, ®s->port[port].ocr2); |
| 256 | |
| 257 | writel(readl(®s->port[port].iconfa2) & ~(3 << (pin * 2)), |
| 258 | ®s->port[port].iconfa2); |
| 259 | writel(readl(®s->port[port].iconfa2) | aout << (pin * 2), |
| 260 | ®s->port[port].iconfa2); |
| 261 | writel(readl(®s->port[port].iconfb2) & ~(3 << (pin * 2)), |
| 262 | ®s->port[port].iconfb2); |
| 263 | writel(readl(®s->port[port].iconfb2) | bout << (pin * 2), |
| 264 | ®s->port[port].iconfb2); |
| 265 | } |
| 266 | } |
Ilya Yanok | 016b702 | 2009-08-11 02:32:09 +0400 | [diff] [blame] | 267 | |
| 268 | #ifdef CONFIG_MXC_UART |
| 269 | void mx27_uart_init_pins(void) |
| 270 | { |
| 271 | int i; |
| 272 | unsigned int mode[] = { |
| 273 | PE12_PF_UART1_TXD, |
| 274 | PE13_PF_UART1_RXD, |
| 275 | }; |
| 276 | |
| 277 | for (i = 0; i < ARRAY_SIZE(mode); i++) |
| 278 | imx_gpio_mode(mode[i]); |
| 279 | |
| 280 | } |
| 281 | #endif /* CONFIG_MXC_UART */ |
| 282 | |
| 283 | #ifdef CONFIG_FEC_MXC |
| 284 | void mx27_fec_init_pins(void) |
| 285 | { |
| 286 | int i; |
| 287 | unsigned int mode[] = { |
| 288 | PD0_AIN_FEC_TXD0, |
| 289 | PD1_AIN_FEC_TXD1, |
| 290 | PD2_AIN_FEC_TXD2, |
| 291 | PD3_AIN_FEC_TXD3, |
| 292 | PD4_AOUT_FEC_RX_ER, |
| 293 | PD5_AOUT_FEC_RXD1, |
| 294 | PD6_AOUT_FEC_RXD2, |
| 295 | PD7_AOUT_FEC_RXD3, |
| 296 | PD8_AF_FEC_MDIO, |
| 297 | PD9_AIN_FEC_MDC | GPIO_PUEN, |
| 298 | PD10_AOUT_FEC_CRS, |
| 299 | PD11_AOUT_FEC_TX_CLK, |
| 300 | PD12_AOUT_FEC_RXD0, |
| 301 | PD13_AOUT_FEC_RX_DV, |
| 302 | PD14_AOUT_FEC_CLR, |
| 303 | PD15_AOUT_FEC_COL, |
| 304 | PD16_AIN_FEC_TX_ER, |
| 305 | PF23_AIN_FEC_TX_EN, |
| 306 | }; |
| 307 | |
| 308 | for (i = 0; i < ARRAY_SIZE(mode); i++) |
| 309 | imx_gpio_mode(mode[i]); |
| 310 | } |
| 311 | #endif /* CONFIG_FEC_MXC */ |
| 312 | |
| 313 | #ifdef CONFIG_MXC_MMC |
| 314 | void mx27_sd2_init_pins(void) |
| 315 | { |
| 316 | int i; |
| 317 | unsigned int mode[] = { |
| 318 | PB4_PF_SD2_D0, |
| 319 | PB5_PF_SD2_D1, |
| 320 | PB6_PF_SD2_D2, |
| 321 | PB7_PF_SD2_D3, |
| 322 | PB8_PF_SD2_CMD, |
| 323 | PB9_PF_SD2_CLK, |
| 324 | }; |
| 325 | |
| 326 | for (i = 0; i < ARRAY_SIZE(mode); i++) |
| 327 | imx_gpio_mode(mode[i]); |
| 328 | |
| 329 | } |
| 330 | #endif /* CONFIG_MXC_MMC */ |