paul@0 | 1 | /* |
paul@35 | 2 | * Generic board initialisation, based on uboot-xburst and xburst-tools. |
paul@0 | 3 | * |
paul@35 | 4 | * Copyright (C) 2000-2009 Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
paul@35 | 5 | * Copyright (C) 2005-2006 Ingenic Semiconductor, <jlwei@ingenic.cn> |
paul@35 | 6 | * Copyright (C) Xiangfu Liu <xiangfu.z@gmail.com> |
paul@0 | 7 | * Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 8 | * |
paul@0 | 9 | * This program is free software; you can redistribute it and/or modify it under |
paul@0 | 10 | * the terms of the GNU General Public License as published by the Free Software |
paul@0 | 11 | * Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 12 | * version. |
paul@0 | 13 | * |
paul@0 | 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 16 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 17 | * details. |
paul@0 | 18 | * |
paul@0 | 19 | * You should have received a copy of the GNU General Public License along with |
paul@0 | 20 | * this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 21 | */ |
paul@0 | 22 | |
paul@35 | 23 | #ifdef CONFIG_CPU_JZ4730 |
paul@35 | 24 | #include "jz4730.h" |
paul@43 | 25 | #include "jz4730_compat.h" |
paul@35 | 26 | #else |
paul@0 | 27 | #include "jz4740.h" |
paul@35 | 28 | #endif |
paul@35 | 29 | |
paul@33 | 30 | #include "sdram.h" |
paul@10 | 31 | #include "usb_boot_defines.h" |
paul@10 | 32 | |
paul@10 | 33 | /* These arguments are initialised by usbboot and are defined in... |
paul@10 | 34 | /etc/xburst-tools/usbboot.cfg. */ |
paul@10 | 35 | |
paul@10 | 36 | struct fw_args *fw_args; |
paul@27 | 37 | volatile u32 FW_CPU_ID; |
paul@27 | 38 | volatile u8 FW_SDRAM_BW16; |
paul@27 | 39 | volatile u8 FW_SDRAM_BANK4; |
paul@27 | 40 | volatile u8 FW_SDRAM_ROW; |
paul@27 | 41 | volatile u8 FW_SDRAM_COL; |
paul@27 | 42 | volatile u8 FW_CONFIG_MOBILE_SDRAM; |
paul@27 | 43 | volatile u8 FW_IS_SHARE; |
paul@10 | 44 | |
paul@10 | 45 | void load_args(void) |
paul@10 | 46 | { |
paul@17 | 47 | /* Get the fw args from memory. See head1.S for the memory layout. */ |
paul@10 | 48 | |
paul@10 | 49 | fw_args = (struct fw_args *)0x80002008; |
paul@27 | 50 | FW_CPU_ID = fw_args->cpu_id ; |
paul@27 | 51 | FW_SDRAM_BW16 = fw_args->bus_width; |
paul@27 | 52 | FW_SDRAM_BANK4 = fw_args->bank_num; |
paul@27 | 53 | FW_SDRAM_ROW = fw_args->row_addr; |
paul@27 | 54 | FW_SDRAM_COL = fw_args->col_addr; |
paul@27 | 55 | FW_CONFIG_MOBILE_SDRAM = fw_args->is_mobile; |
paul@27 | 56 | FW_IS_SHARE = fw_args->is_busshare; |
paul@10 | 57 | } |
paul@10 | 58 | |
paul@10 | 59 | /* Initialisation functions. */ |
paul@0 | 60 | |
paul@0 | 61 | void gpio_init(void) |
paul@0 | 62 | { |
paul@45 | 63 | #ifndef CONFIG_CPU_JZ4730 |
paul@0 | 64 | /* |
paul@0 | 65 | * Initialize NAND Flash Pins |
paul@0 | 66 | */ |
paul@0 | 67 | __gpio_as_nand(); |
paul@0 | 68 | |
paul@0 | 69 | /* |
paul@0 | 70 | * Initialize SDRAM pins |
paul@0 | 71 | */ |
paul@17 | 72 | __gpio_as_sdram_16bit_4720(); |
paul@45 | 73 | #endif |
paul@6 | 74 | } |
paul@0 | 75 | |
paul@0 | 76 | void pll_init(void) |
paul@0 | 77 | { |
paul@0 | 78 | register unsigned int cfcr, plcr1; |
paul@0 | 79 | int nf, pllout2; |
paul@0 | 80 | |
paul@17 | 81 | /* See CPCCR (Clock Control Register). |
paul@17 | 82 | * 0 == same frequency; 2 == f/3 |
paul@17 | 83 | */ |
paul@17 | 84 | |
paul@0 | 85 | cfcr = CPM_CPCCR_CLKOEN | |
paul@17 | 86 | CPM_CPCCR_PCS | |
paul@17 | 87 | (0 << CPM_CPCCR_CDIV_BIT) | |
paul@17 | 88 | (2 << CPM_CPCCR_HDIV_BIT) | |
paul@17 | 89 | (2 << CPM_CPCCR_PDIV_BIT) | |
paul@17 | 90 | (2 << CPM_CPCCR_MDIV_BIT) | |
paul@17 | 91 | (2 << CPM_CPCCR_LDIV_BIT); |
paul@0 | 92 | |
paul@17 | 93 | /* Init USB Host clock. |
paul@17 | 94 | * Desired frequency == 48MHz |
paul@17 | 95 | */ |
paul@17 | 96 | |
paul@33 | 97 | #ifdef CONFIG_CPU_JZ4730 |
paul@33 | 98 | cfcr |= ((CFG_CPU_SPEED / 48000000 - 1) << 25); |
paul@33 | 99 | #else |
paul@33 | 100 | /* Determine the divider clock output based on the PCS bit. */ |
paul@33 | 101 | pllout2 = (cfcr & CPM_CPCCR_PCS) ? CONFIG_SYS_CPU_SPEED : (CONFIG_SYS_CPU_SPEED / 2); |
paul@33 | 102 | |
paul@33 | 103 | /* Divisor == UHCCDR + 1 */ |
paul@0 | 104 | REG_CPM_UHCCDR = pllout2 / 48000000 - 1; |
paul@33 | 105 | #endif |
paul@0 | 106 | |
paul@17 | 107 | nf = CONFIG_SYS_CPU_SPEED * 2 / CONFIG_SYS_EXTAL; |
paul@17 | 108 | plcr1 = ((nf - 2) << CPM_CPPCR_PLLM_BIT) | /* FD */ |
paul@17 | 109 | (0 << CPM_CPPCR_PLLN_BIT) | /* RD=0, NR=2 */ |
paul@17 | 110 | (0 << CPM_CPPCR_PLLOD_BIT) | /* OD=0, NO=1 */ |
paul@17 | 111 | CPM_CPPCR_PLLEN; /* enable PLL */ |
paul@0 | 112 | |
paul@17 | 113 | /* Update PLL and wait. */ |
paul@17 | 114 | |
paul@0 | 115 | REG_CPM_CPCCR = cfcr; |
paul@0 | 116 | REG_CPM_CPPCR = plcr1; |
paul@17 | 117 | while (!__cpm_pll_is_on()); |
paul@0 | 118 | } |
paul@0 | 119 | |
paul@0 | 120 | void sdram_init(void) |
paul@0 | 121 | { |
paul@0 | 122 | register unsigned int dmcr0, dmcr, sdmode, tmp, cpu_clk, mem_clk, ns; |
paul@17 | 123 | unsigned int pllout = __cpm_get_pllout(); |
paul@0 | 124 | |
paul@0 | 125 | unsigned int cas_latency_sdmr[2] = { |
paul@0 | 126 | EMC_SDMR_CAS_2, |
paul@0 | 127 | EMC_SDMR_CAS_3, |
paul@0 | 128 | }; |
paul@0 | 129 | |
paul@0 | 130 | unsigned int cas_latency_dmcr[2] = { |
paul@0 | 131 | 1 << EMC_DMCR_TCL_BIT, /* CAS latency is 2 */ |
paul@0 | 132 | 2 << EMC_DMCR_TCL_BIT /* CAS latency is 3 */ |
paul@0 | 133 | }; |
paul@0 | 134 | |
paul@17 | 135 | /* Divisors for CPCCR values. */ |
paul@17 | 136 | |
paul@0 | 137 | int div[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32}; |
paul@0 | 138 | |
paul@17 | 139 | cpu_clk = pllout / div[__cpm_get_cdiv()]; |
paul@17 | 140 | mem_clk = pllout / div[__cpm_get_mdiv()]; |
paul@0 | 141 | |
paul@0 | 142 | REG_EMC_BCR = 0; /* Disable bus release */ |
paul@0 | 143 | REG_EMC_RTCSR = 0; /* Disable clock for counting */ |
paul@0 | 144 | |
paul@0 | 145 | /* Fault DMCR value for mode register setting*/ |
paul@30 | 146 | dmcr0 = (0<<EMC_DMCR_RA_BIT) | |
paul@30 | 147 | (0<<EMC_DMCR_CA_BIT) | |
paul@30 | 148 | (0<<EMC_DMCR_BA_BIT) | |
paul@27 | 149 | (FW_SDRAM_BW16<<EMC_DMCR_BW_BIT) | |
paul@0 | 150 | EMC_DMCR_EPIN | |
paul@0 | 151 | cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)]; |
paul@0 | 152 | |
paul@0 | 153 | /* Basic DMCR value */ |
paul@30 | 154 | dmcr = ((FW_SDRAM_ROW-SDRAM_ROW0)<<EMC_DMCR_RA_BIT) | |
paul@30 | 155 | ((FW_SDRAM_COL-SDRAM_COL0)<<EMC_DMCR_CA_BIT) | |
paul@30 | 156 | ((FW_SDRAM_BANK4-SDRAM_BANK40)<<EMC_DMCR_BA_BIT) | |
paul@27 | 157 | (FW_SDRAM_BW16<<EMC_DMCR_BW_BIT) | |
paul@0 | 158 | EMC_DMCR_EPIN | |
paul@0 | 159 | cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)]; |
paul@0 | 160 | |
paul@0 | 161 | /* SDRAM timimg */ |
paul@0 | 162 | ns = 1000000000 / mem_clk; |
paul@0 | 163 | tmp = SDRAM_TRAS/ns; |
paul@0 | 164 | if (tmp < 4) tmp = 4; |
paul@0 | 165 | if (tmp > 11) tmp = 11; |
paul@0 | 166 | dmcr |= ((tmp-4) << EMC_DMCR_TRAS_BIT); |
paul@0 | 167 | tmp = SDRAM_RCD/ns; |
paul@0 | 168 | if (tmp > 3) tmp = 3; |
paul@0 | 169 | dmcr |= (tmp << EMC_DMCR_RCD_BIT); |
paul@0 | 170 | tmp = SDRAM_TPC/ns; |
paul@0 | 171 | if (tmp > 7) tmp = 7; |
paul@0 | 172 | dmcr |= (tmp << EMC_DMCR_TPC_BIT); |
paul@0 | 173 | tmp = SDRAM_TRWL/ns; |
paul@0 | 174 | if (tmp > 3) tmp = 3; |
paul@0 | 175 | dmcr |= (tmp << EMC_DMCR_TRWL_BIT); |
paul@0 | 176 | tmp = (SDRAM_TRAS + SDRAM_TPC)/ns; |
paul@0 | 177 | if (tmp > 14) tmp = 14; |
paul@0 | 178 | dmcr |= (((tmp + 1) >> 1) << EMC_DMCR_TRC_BIT); |
paul@0 | 179 | |
paul@0 | 180 | /* SDRAM mode value */ |
paul@0 | 181 | sdmode = EMC_SDMR_BT_SEQ | |
paul@0 | 182 | EMC_SDMR_OM_NORMAL | |
paul@0 | 183 | EMC_SDMR_BL_4 | |
paul@0 | 184 | cas_latency_sdmr[((SDRAM_CASL == 3) ? 1 : 0)]; |
paul@0 | 185 | |
paul@33 | 186 | /* jz4730 additional measures */ |
paul@33 | 187 | #ifdef CONFIG_CPU_JZ4730 |
paul@33 | 188 | if (FW_SDRAM_BW16) |
paul@33 | 189 | sdmode <<= 1; |
paul@33 | 190 | else |
paul@33 | 191 | sdmode <<= 2; |
paul@33 | 192 | #endif |
paul@33 | 193 | |
paul@0 | 194 | /* Stage 1. Precharge all banks by writing SDMR with DMCR.MRSET=0 */ |
paul@0 | 195 | REG_EMC_DMCR = dmcr; |
paul@0 | 196 | REG8(EMC_SDMR0|sdmode) = 0; |
paul@0 | 197 | |
paul@33 | 198 | /* jz4730 additional measures */ |
paul@33 | 199 | #ifdef CONFIG_CPU_JZ4730 |
paul@33 | 200 | REG8(EMC_SDMR1|sdmode) = 0; |
paul@33 | 201 | #endif |
paul@33 | 202 | |
paul@0 | 203 | /* Wait for precharge, > 200us */ |
paul@0 | 204 | tmp = (cpu_clk / 1000000) * 1000; |
paul@0 | 205 | while (tmp--); |
paul@0 | 206 | |
paul@0 | 207 | /* Stage 2. Enable auto-refresh */ |
paul@0 | 208 | REG_EMC_DMCR = dmcr | EMC_DMCR_RFSH; |
paul@0 | 209 | |
paul@0 | 210 | tmp = SDRAM_TREF/ns; |
paul@0 | 211 | tmp = tmp/64 + 1; |
paul@0 | 212 | if (tmp > 0xff) tmp = 0xff; |
paul@0 | 213 | REG_EMC_RTCOR = tmp; |
paul@0 | 214 | REG_EMC_RTCNT = 0; |
paul@0 | 215 | REG_EMC_RTCSR = EMC_RTCSR_CKS_64; /* Divisor is 64, CKO/64 */ |
paul@0 | 216 | |
paul@0 | 217 | /* Wait for number of auto-refresh cycles */ |
paul@0 | 218 | tmp = (cpu_clk / 1000000) * 1000; |
paul@0 | 219 | while (tmp--); |
paul@0 | 220 | |
paul@0 | 221 | /* Stage 3. Mode Register Set */ |
paul@0 | 222 | REG_EMC_DMCR = dmcr0 | EMC_DMCR_RFSH | EMC_DMCR_MRSET; |
paul@0 | 223 | REG8(EMC_SDMR0|sdmode) = 0; |
paul@0 | 224 | |
paul@33 | 225 | /* jz4730 additional measures */ |
paul@33 | 226 | #ifdef CONFIG_CPU_JZ4730 |
paul@33 | 227 | REG8(EMC_SDMR1|sdmode) = 0; |
paul@33 | 228 | #endif |
paul@33 | 229 | |
paul@0 | 230 | /* Set back to basic DMCR value */ |
paul@0 | 231 | REG_EMC_DMCR = dmcr | EMC_DMCR_RFSH | EMC_DMCR_MRSET; |
paul@0 | 232 | |
paul@0 | 233 | /* everything is ok now */ |
paul@0 | 234 | } |