Root/usbboot/xburst_stage1/board_4740.c

1/*
2 * device board
3 *
4 * Copyright 2009 (C) Qi Hardware Inc.,
5 * Author: Xiangfu Liu <xiangfu@qi-hardware.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 3 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA
20 */
21
22#include "jz4740.h"
23#include "configs.h"
24
25void gpio_init_4740(void)
26{
27    /*
28     * Initialize SDRAM pins
29     */
30#if 0
31    /* PORT A: D0 ~ D31 */
32    REG_GPIO_PXFUNS(0) = 0xffffffff;
33    REG_GPIO_PXSELC(0) = 0xffffffff;
34
35    /* PORT B: A0 ~ A16, DCS#, RAS#, CAS#, CKE#, RDWE#, CKO#, WE0# */
36    REG_GPIO_PXFUNS(1) = 0x81f9ffff;
37    REG_GPIO_PXSELC(1) = 0x81f9ffff;
38
39    /* PORT C: WE1#, WE2#, WE3# */
40    REG_GPIO_PXFUNS(2) = 0x07000000;
41    REG_GPIO_PXSELC(2) = 0x07000000;
42
43    /*
44     * Initialize Static Memory Pins
45     */
46
47    /* CS4# */
48    REG_GPIO_PXFUNS(1) = 0x10000000;
49    REG_GPIO_PXSELC(1) = 0x10000000;
50
51    /*
52     * Initialize UART0 pins
53     */
54
55    /* PORT D: TXD/RXD */
56    REG_GPIO_PXFUNS(3) = 0x06000000;
57    REG_GPIO_PXSELS(3) = 0x06000000;
58#endif
59
60    __gpio_as_nand();
61    /*
62     * Initialize SDRAM pins
63     */
64    __gpio_as_sdram_32bit();
65
66    /*
67     * Initialize UART0 pins
68     */
69    __gpio_as_uart0();
70    __gpio_as_uart1();
71}
72
73void pll_init_4740(void)
74{
75    register unsigned int cfcr, plcr1;
76    int n2FR[33] = {
77        0, 0, 1, 2, 3, 0, 4, 0, 5, 0, 0, 0, 6, 0, 0, 0,
78        7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0,
79        9
80    };
81    /* int div[5] = {1, 4, 4, 4, 4}; */ /* divisors of I:S:P:L:M */
82    int nf, pllout2;
83
84    cfcr = CPM_CPCCR_CLKOEN |
85        (n2FR[1] << CPM_CPCCR_CDIV_BIT) |
86        (n2FR[PHM_DIV] << CPM_CPCCR_HDIV_BIT) |
87        (n2FR[PHM_DIV] << CPM_CPCCR_PDIV_BIT) |
88        (n2FR[PHM_DIV] << CPM_CPCCR_MDIV_BIT) |
89        (n2FR[PHM_DIV] << CPM_CPCCR_LDIV_BIT);
90
91    pllout2 = (cfcr & CPM_CPCCR_PCS) ? CFG_CPU_SPEED : (CFG_CPU_SPEED / 2);
92
93    /* Init UHC clock */
94    REG_CPM_UHCCDR = pllout2 / 48000000 - 1;
95
96    nf = CFG_CPU_SPEED * 2 / CFG_EXTAL;
97    plcr1 = ((nf - 2) << CPM_CPPCR_PLLM_BIT) | /* FD */
98        (0 << CPM_CPPCR_PLLN_BIT) | /* RD=0, NR=2 */
99        (0 << CPM_CPPCR_PLLOD_BIT) | /* OD=0, NO=1 */
100        (0x20 << CPM_CPPCR_PLLST_BIT) | /* PLL stable time */
101        CPM_CPPCR_PLLEN; /* enable PLL */
102
103    /* init PLL */
104    REG_CPM_CPCCR = cfcr;
105    REG_CPM_CPPCR = plcr1;
106}
107
108void sdram_init_4740(void)
109{
110    register unsigned int dmcr0, dmcr, sdmode, tmp, cpu_clk, mem_clk, ns;
111
112    unsigned int cas_latency_sdmr[2] = {
113        EMC_SDMR_CAS_2,
114        EMC_SDMR_CAS_3,
115    };
116
117    unsigned int cas_latency_dmcr[2] = {
118        1 << EMC_DMCR_TCL_BIT, /* CAS latency is 2 */
119        2 << EMC_DMCR_TCL_BIT /* CAS latency is 3 */
120    };
121
122    int div[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};
123
124    if (SDRAM_BW16 == 0xff) {
125        serial_puts("hura");
126        return;
127    }
128
129    serial_put_hex(0xf00);
130    cpu_clk = CFG_CPU_SPEED;
131    mem_clk = cpu_clk * div[__cpm_get_cdiv()] / div[__cpm_get_mdiv()];
132    serial_put_hex(0xf01);
133
134    REG_EMC_BCR = 0; /* Disable bus release */
135    serial_put_hex(0xf02);
136    REG_EMC_RTCSR = 0; /* Disable clock for counting */
137    serial_put_hex(0xf03);
138
139    /* Fault DMCR value for mode register setting*/
140#define SDRAM_ROW0 11
141#define SDRAM_COL0 8
142#define SDRAM_BANK40 0
143
144    serial_put_hex(0xf04);
145    dmcr0 = ((SDRAM_ROW0-11)<<EMC_DMCR_RA_BIT) |
146        ((SDRAM_COL0-8)<<EMC_DMCR_CA_BIT) |
147        (SDRAM_BANK40<<EMC_DMCR_BA_BIT) |
148        (SDRAM_BW16<<EMC_DMCR_BW_BIT) |
149        EMC_DMCR_EPIN |
150        cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];
151
152    serial_put_hex(0xf05);
153    /* Basic DMCR value */
154    dmcr = ((SDRAM_ROW-11)<<EMC_DMCR_RA_BIT) |
155        ((SDRAM_COL-8)<<EMC_DMCR_CA_BIT) |
156        (SDRAM_BANK4<<EMC_DMCR_BA_BIT) |
157        (SDRAM_BW16<<EMC_DMCR_BW_BIT) |
158        EMC_DMCR_EPIN |
159        cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];
160
161    serial_put_hex(0xf06);
162    /* SDRAM timimg */
163    ns = 1000000000 / mem_clk;
164    tmp = SDRAM_TRAS/ns;
165    if (tmp < 4) tmp = 4;
166    if (tmp > 11) tmp = 11;
167    dmcr |= ((tmp-4) << EMC_DMCR_TRAS_BIT);
168    tmp = SDRAM_RCD/ns;
169    if (tmp > 3) tmp = 3;
170    dmcr |= (tmp << EMC_DMCR_RCD_BIT);
171    tmp = SDRAM_TPC/ns;
172    if (tmp > 7) tmp = 7;
173    dmcr |= (tmp << EMC_DMCR_TPC_BIT);
174    tmp = SDRAM_TRWL/ns;
175    if (tmp > 3) tmp = 3;
176    dmcr |= (tmp << EMC_DMCR_TRWL_BIT);
177    tmp = (SDRAM_TRAS + SDRAM_TPC)/ns;
178    if (tmp > 14) tmp = 14;
179    dmcr |= (((tmp + 1) >> 1) << EMC_DMCR_TRC_BIT);
180
181    /* SDRAM mode value */
182    sdmode = EMC_SDMR_BT_SEQ |
183         EMC_SDMR_OM_NORMAL |
184         EMC_SDMR_BL_4 |
185         cas_latency_sdmr[((SDRAM_CASL == 3) ? 1 : 0)];
186
187    serial_put_hex(0xf07);
188    /* Stage 1. Precharge all banks by writing SDMR with DMCR.MRSET=0 */
189    serial_put_hex(dmcr);
190    serial_put_hex(REG_EMC_DMCR);
191    REG_EMC_DMCR = dmcr;
192    REG8(EMC_SDMR0|sdmode) = 0;
193
194    /* Wait for precharge, > 200us */
195    tmp = (cpu_clk / 1000000) * 1000;
196    while (tmp--);
197    serial_put_hex(0xf08);
198
199    /* Stage 2. Enable auto-refresh */
200    REG_EMC_DMCR = dmcr | EMC_DMCR_RFSH;
201    serial_put_hex(0xf09);
202    tmp = SDRAM_TREF/ns;
203    serial_put_hex(0xf10);
204    tmp = tmp/64 + 1;
205    serial_put_hex(0xf11);
206    if (tmp > 0xff) tmp = 0xff;
207    serial_put_hex(tmp);
208/* REG_EMC_RTCOR = tmp;*/
209    serial_put_hex(0xf10);
210    REG_EMC_RTCNT = 0;
211    serial_put_hex(0xf11);
212    REG_EMC_RTCSR = EMC_RTCSR_CKS_64; /* Divisor is 64, CKO/64 */
213    serial_put_hex(0xf12);
214
215    /* Wait for number of auto-refresh cycles */
216    tmp = (cpu_clk / 1000000) * 1000;
217    while (tmp--);
218
219     /* Stage 3. Mode Register Set */
220    REG_EMC_DMCR = dmcr0 | EMC_DMCR_RFSH | EMC_DMCR_MRSET;
221    REG8(EMC_SDMR0|sdmode) = 0;
222
223    serial_put_hex(0xf11);
224        /* Set back to basic DMCR value */
225    REG_EMC_DMCR = dmcr | EMC_DMCR_RFSH | EMC_DMCR_MRSET;
226    serial_put_hex(0xf12);
227    /* everything is ok now */
228}
229
230void serial_setbrg_4740(void)
231{
232    volatile u8 *uart_lcr = (volatile u8 *)(UART_BASE + OFF_LCR);
233    volatile u8 *uart_dlhr = (volatile u8 *)(UART_BASE + OFF_DLHR);
234    volatile u8 *uart_dllr = (volatile u8 *)(UART_BASE + OFF_DLLR);
235    u32 baud_div, tmp;
236
237    baud_div = CFG_EXTAL / 16 / CONFIG_BAUDRATE;
238    tmp = *uart_lcr;
239    tmp |= UART_LCR_DLAB;
240    *uart_lcr = tmp;
241
242    *uart_dlhr = (baud_div >> 8) & 0xff;
243    *uart_dllr = baud_div & 0xff;
244
245    tmp &= ~UART_LCR_DLAB;
246    *uart_lcr = tmp;
247}
248

Archive Download this file



interactive