summaryrefslogtreecommitdiff
path: root/arch/mips/cpu/xburst/jz4740.c
blob: c0b9817ab9765d8e73cb8f27d1e273b347b4fb21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Jz4740 common routines
 * Copyright (c) 2006 Ingenic Semiconductor, <jlwei@ingenic.cn>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <config.h>
#include <common.h>
#include <asm/io.h>
#include <asm/jz4740.h>

void enable_interrupts(void)
{
}

int disable_interrupts(void)
{
	return 0;
}

/*
 * PLL output clock = EXTAL * NF / (NR * NO)
 * NF = FD + 2, NR = RD + 2
 * NO = 1 (if OD = 0), NO = 2 (if OD = 1 or 2), NO = 4 (if OD = 3)
 */
void pll_init(void)
{
	struct jz4740_cpm *cpm = (struct jz4740_cpm *)JZ4740_CPM_BASE;

	register unsigned int cfcr, plcr1;
	int n2FR[33] = {
		0, 0, 1, 2, 3, 0, 4, 0, 5, 0, 0, 0, 6, 0, 0, 0,
		7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0,
		9
	};
	int div[5] = {1, 3, 3, 3, 3}; /* divisors of I:S:P:L:M */
	int nf, pllout2;

	cfcr =	CPM_CPCCR_CLKOEN |
		CPM_CPCCR_PCS |
		(n2FR[div[0]] << CPM_CPCCR_CDIV_BIT) |
		(n2FR[div[1]] << CPM_CPCCR_HDIV_BIT) |
		(n2FR[div[2]] << CPM_CPCCR_PDIV_BIT) |
		(n2FR[div[3]] << CPM_CPCCR_MDIV_BIT) |
		(n2FR[div[4]] << CPM_CPCCR_LDIV_BIT);

	pllout2 = (cfcr & CPM_CPCCR_PCS) ?
		CONFIG_SYS_CPU_SPEED : (CONFIG_SYS_CPU_SPEED / 2);

	/* Init USB Host clock, pllout2 must be n*48MHz */
	writel(pllout2 / 48000000 - 1, &cpm->uhccdr);

	nf = CONFIG_SYS_CPU_SPEED * 2 / CONFIG_SYS_EXTAL;
	plcr1 = ((nf - 2) << CPM_CPPCR_PLLM_BIT) | /* FD */
		(0 << CPM_CPPCR_PLLN_BIT) |	/* RD=0, NR=2 */
		(0 << CPM_CPPCR_PLLOD_BIT) |	/* OD=0, NO=1 */
		(0x20 << CPM_CPPCR_PLLST_BIT) |	/* PLL stable time */
		CPM_CPPCR_PLLEN;		/* enable PLL */

	/* init PLL */
	writel(cfcr, &cpm->cpccr);
	writel(plcr1, &cpm->cppcr);
}

void sdram_init(void)
{
	struct jz4740_emc *emc = (struct jz4740_emc *)JZ4740_EMC_BASE;

	register unsigned int dmcr0, dmcr, sdmode, tmp, cpu_clk, mem_clk, ns;

	unsigned int cas_latency_sdmr[2] = {
		EMC_SDMR_CAS_2,
		EMC_SDMR_CAS_3,
	};

	unsigned int cas_latency_dmcr[2] = {
		1 << EMC_DMCR_TCL_BIT,	/* CAS latency is 2 */
		2 << EMC_DMCR_TCL_BIT	/* CAS latency is 3 */
	};

	int div[] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};

	cpu_clk = CONFIG_SYS_CPU_SPEED;
	mem_clk = cpu_clk * div[__cpm_get_cdiv()] / div[__cpm_get_mdiv()];

	writel(0, &emc->bcr);	/* Disable bus release */
	writew(0, &emc->rtcsr);	/* Disable clock for counting */

	/* Fault DMCR value for mode register setting*/
#define SDRAM_ROW0	11
#define SDRAM_COL0	8
#define SDRAM_BANK40	0

	dmcr0 = ((SDRAM_ROW0 - 11) << EMC_DMCR_RA_BIT) |
		((SDRAM_COL0 - 8) << EMC_DMCR_CA_BIT) |
		(SDRAM_BANK40 << EMC_DMCR_BA_BIT) |
		(SDRAM_BW16 << EMC_DMCR_BW_BIT) |
		EMC_DMCR_EPIN |
		cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];

	/* Basic DMCR value */
	dmcr = ((SDRAM_ROW - 11) << EMC_DMCR_RA_BIT) |
		((SDRAM_COL - 8) << EMC_DMCR_CA_BIT) |
		(SDRAM_BANK4 << EMC_DMCR_BA_BIT) |
		(SDRAM_BW16 << EMC_DMCR_BW_BIT) |
		EMC_DMCR_EPIN |
		cas_latency_dmcr[((SDRAM_CASL == 3) ? 1 : 0)];

	/* SDRAM timimg */
	ns = 1000000000 / mem_clk;
	tmp = SDRAM_TRAS / ns;
	if (tmp < 4)
		tmp = 4;
	if (tmp > 11)
		tmp = 11;
	dmcr |= (tmp - 4) << EMC_DMCR_TRAS_BIT;
	tmp = SDRAM_RCD / ns;

	if (tmp > 3)
		tmp = 3;
	dmcr |= tmp << EMC_DMCR_RCD_BIT;
	tmp = SDRAM_TPC / ns;

	if (tmp > 7)
		tmp = 7;
	dmcr |= tmp << EMC_DMCR_TPC_BIT;
	tmp = SDRAM_TRWL / ns;

	if (tmp > 3)
		tmp = 3;
	dmcr |= tmp << EMC_DMCR_TRWL_BIT;
	tmp = (SDRAM_TRAS + SDRAM_TPC) / ns;

	if (tmp > 14)
		tmp = 14;
	dmcr |= ((tmp + 1) >> 1) << EMC_DMCR_TRC_BIT;

	/* SDRAM mode value */
	sdmode = EMC_SDMR_BT_SEQ |
		 EMC_SDMR_OM_NORMAL |
		 EMC_SDMR_BL_4 |
		 cas_latency_sdmr[((SDRAM_CASL == 3) ? 1 : 0)];

	/* Stage 1. Precharge all banks by writing SDMR with DMCR.MRSET=0 */
	writel(dmcr, &emc->dmcr);
	writeb(0, JZ4740_EMC_SDMR0 | sdmode);

	/* Wait for precharge, > 200us */
	tmp = (cpu_clk / 1000000) * 1000;
	while (tmp--)
		;

	/* Stage 2. Enable auto-refresh */
	writel(dmcr | EMC_DMCR_RFSH, &emc->dmcr);

	tmp = SDRAM_TREF / ns;
	tmp = tmp / 64 + 1;
	if (tmp > 0xff)
		tmp = 0xff;
	writew(tmp, &emc->rtcor);
	writew(0, &emc->rtcnt);
	/* Divisor is 64, CKO/64 */
	writew(EMC_RTCSR_CKS_64, &emc->rtcsr);

	/* Wait for number of auto-refresh cycles */
	tmp = (cpu_clk / 1000000) * 1000;
	while (tmp--)
		;

	/* Stage 3. Mode Register Set */
	writel(dmcr0 | EMC_DMCR_RFSH | EMC_DMCR_MRSET, &emc->dmcr);
	writeb(0, JZ4740_EMC_SDMR0 | sdmode);

	/* Set back to basic DMCR value */
	writel(dmcr | EMC_DMCR_RFSH | EMC_DMCR_MRSET, &emc->dmcr);

	/* everything is ok now */
}

DECLARE_GLOBAL_DATA_PTR;

void calc_clocks(void)
{
	unsigned int pllout;
	unsigned int div[10] = {1, 2, 3, 4, 6, 8, 12, 16, 24, 32};

	pllout = __cpm_get_pllout();

	gd->cpu_clk = pllout / div[__cpm_get_cdiv()];
	gd->sys_clk = pllout / div[__cpm_get_hdiv()];
	gd->per_clk = pllout / div[__cpm_get_pdiv()];
	gd->mem_clk = pllout / div[__cpm_get_mdiv()];
	gd->dev_clk = CONFIG_SYS_EXTAL;
}

void rtc_init(void)
{
	struct jz4740_rtc *rtc = (struct jz4740_rtc *)JZ4740_RTC_BASE;

	while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
		;
	writel(readl(&rtc->rcr) | RTC_RCR_AE, &rtc->rcr); /* enable alarm */

	while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
		;
	writel(0x00007fff, &rtc->rgr); /* type value */

	while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
		;
	writel(0x0000ffe0, &rtc->hwfcr); /* Power on delay 2s */

	while (!(readl(&rtc->rcr) & RTC_RCR_WRDY))
		;
	writel(0x00000fe0, &rtc->hrcr); /* reset delay 125ms */
}

/* U-Boot common routines */
phys_size_t initdram(int board_type)
{
	struct jz4740_emc *emc = (struct jz4740_emc *)JZ4740_EMC_BASE;
	u32 dmcr;
	u32 rows, cols, dw, banks;
	ulong size;

	dmcr = readl(&emc->dmcr);
	rows = 11 + ((dmcr & EMC_DMCR_RA_MASK) >> EMC_DMCR_RA_BIT);
	cols = 8 + ((dmcr & EMC_DMCR_CA_MASK) >> EMC_DMCR_CA_BIT);
	dw = (dmcr & EMC_DMCR_BW) ? 2 : 4;
	banks = (dmcr & EMC_DMCR_BA) ? 4 : 2;

	size = (1 << (rows + cols)) * dw * banks;

	return size;
}