blob: 5e5702cd410dda15847cf152a6c9e90d2708f2cd [file] [log] [blame]
Tero Kristo20006552009-11-22 10:11:36 -08001/*
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -08002 * SDRC register values for Nokia boards
Tero Kristo20006552009-11-22 10:11:36 -08003 *
Igor Dmitrievef211dc2011-12-13 10:48:54 -08004 * Copyright (C) 2008, 2010-2011 Nokia Corporation
Tero Kristo20006552009-11-22 10:11:36 -08005 *
6 * Lauri Leukkunen <lauri.leukkunen@nokia.com>
7 *
8 * Original code by Juha Yrjola <juha.yrjola@solidboot.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/kernel.h>
16#include <linux/clk.h>
17#include <linux/err.h>
18#include <linux/io.h>
19
Tony Lindgren4e653312011-11-10 22:45:17 +010020#include "common.h"
Tero Kristo20006552009-11-22 10:11:36 -080021#include <plat/clock.h>
Tero Kristo20006552009-11-22 10:11:36 -080022
Aaro Koskinenfcd8d8462010-12-17 15:13:44 -080023#include "sdram-nokia.h"
Paul Walmsley3e6ece12012-10-17 00:46:45 +000024#include "sdrc.h"
Tero Kristo20006552009-11-22 10:11:36 -080025
26/* In picoseconds, except for tREF (ns), tXP, tCKE, tWTR (clks) */
27struct sdram_timings {
28 u32 casl;
29 u32 tDAL;
30 u32 tDPL;
31 u32 tRRD;
32 u32 tRCD;
33 u32 tRP;
34 u32 tRAS;
35 u32 tRC;
36 u32 tRFC;
37 u32 tXSR;
38
39 u32 tREF; /* in ns */
40
41 u32 tXP;
42 u32 tCKE;
43 u32 tWTR;
44};
45
Aaro Koskinen20dbeb12010-12-17 15:13:46 -080046static const struct sdram_timings nokia_97dot6mhz_timings[] = {
47 {
48 .casl = 3,
49 .tDAL = 30725,
50 .tDPL = 15362,
51 .tRRD = 10241,
52 .tRCD = 20483,
53 .tRP = 15362,
54 .tRAS = 40967,
55 .tRC = 56330,
56 .tRFC = 138266,
57 .tXSR = 204839,
58
59 .tREF = 7798,
60
61 .tXP = 2,
62 .tCKE = 4,
63 .tWTR = 2,
64 },
65};
66
Aaro Koskinenfbd208e2010-12-17 15:13:45 -080067static const struct sdram_timings nokia_166mhz_timings[] = {
Tero Kristo20006552009-11-22 10:11:36 -080068 {
69 .casl = 3,
70 .tDAL = 33000,
71 .tDPL = 15000,
72 .tRRD = 12000,
73 .tRCD = 22500,
74 .tRP = 18000,
75 .tRAS = 42000,
76 .tRC = 66000,
77 .tRFC = 138000,
78 .tXSR = 200000,
79
80 .tREF = 7800,
81
82 .tXP = 2,
83 .tCKE = 2,
84 .tWTR = 2
85 },
86};
87
Aaro Koskinen20dbeb12010-12-17 15:13:46 -080088static const struct sdram_timings nokia_195dot2mhz_timings[] = {
89 {
90 .casl = 3,
91 .tDAL = 30725,
92 .tDPL = 15362,
93 .tRRD = 10241,
94 .tRCD = 20483,
95 .tRP = 15362,
96 .tRAS = 40967,
97 .tRC = 56330,
98 .tRFC = 138266,
99 .tXSR = 204839,
100
101 .tREF = 7752,
102
103 .tXP = 2,
104 .tCKE = 4,
105 .tWTR = 2,
106 },
107};
108
Igor Dmitrievef211dc2011-12-13 10:48:54 -0800109static const struct sdram_timings nokia_200mhz_timings[] = {
110 {
111 .casl = 3,
112 .tDAL = 30000,
113 .tDPL = 15000,
114 .tRRD = 10000,
115 .tRCD = 20000,
116 .tRP = 15000,
117 .tRAS = 40000,
118 .tRC = 55000,
119 .tRFC = 140000,
120 .tXSR = 200000,
121
122 .tREF = 7800,
123
124 .tXP = 2,
125 .tCKE = 4,
126 .tWTR = 2
127 },
128};
129
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800130static const struct {
131 long rate;
132 struct sdram_timings const *data;
133} nokia_timings[] = {
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800134 { 83000000, nokia_166mhz_timings },
Aaro Koskinen20dbeb12010-12-17 15:13:46 -0800135 { 97600000, nokia_97dot6mhz_timings },
Igor Dmitrievef211dc2011-12-13 10:48:54 -0800136 { 100000000, nokia_200mhz_timings },
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800137 { 166000000, nokia_166mhz_timings },
Aaro Koskinen20dbeb12010-12-17 15:13:46 -0800138 { 195200000, nokia_195dot2mhz_timings },
Igor Dmitrievef211dc2011-12-13 10:48:54 -0800139 { 200000000, nokia_200mhz_timings },
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800140};
141static struct omap_sdrc_params nokia_sdrc_params[ARRAY_SIZE(nokia_timings) + 1];
142
Tero Kristo20006552009-11-22 10:11:36 -0800143static unsigned long sdrc_get_fclk_period(long rate)
144{
145 /* In picoseconds */
146 return 1000000000 / rate;
147}
148
149static unsigned int sdrc_ps_to_ticks(unsigned int time_ps, long rate)
150{
151 unsigned long tick_ps;
152
153 /* Calculate in picosecs to yield more exact results */
154 tick_ps = sdrc_get_fclk_period(rate);
155
156 return (time_ps + tick_ps - 1) / tick_ps;
157}
158#undef DEBUG
159#ifdef DEBUG
160static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
161 int ticks, long rate, const char *name)
162#else
163static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
164 int ticks)
165#endif
166{
167 int mask, nr_bits;
168
169 nr_bits = end_bit - st_bit + 1;
170 if (ticks >= 1 << nr_bits)
171 return -1;
172 mask = (1 << nr_bits) - 1;
173 *regval &= ~(mask << st_bit);
174 *regval |= ticks << st_bit;
175#ifdef DEBUG
176 printk(KERN_INFO "SDRC %s: %i ticks %i ns\n", name, ticks,
177 (unsigned int)sdrc_get_fclk_period(rate) * ticks /
178 1000);
179#endif
180
181 return 0;
182}
183
184#ifdef DEBUG
185#define SDRC_SET_ONE(reg, st, end, field, rate) \
186 if (set_sdrc_timing_regval((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800187 memory_timings->field, (rate), #field) < 0) \
Tero Kristo20006552009-11-22 10:11:36 -0800188 err = -1;
189#else
190#define SDRC_SET_ONE(reg, st, end, field, rate) \
191 if (set_sdrc_timing_regval((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800192 memory_timings->field) < 0) \
Tero Kristo20006552009-11-22 10:11:36 -0800193 err = -1;
194#endif
195
196#ifdef DEBUG
197static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
198 int time, long rate, const char *name)
199#else
200static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
201 int time, long rate)
202#endif
203{
204 int ticks, ret;
205 ret = 0;
206
207 if (time == 0)
208 ticks = 0;
209 else
210 ticks = sdrc_ps_to_ticks(time, rate);
211
212#ifdef DEBUG
213 ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks,
214 rate, name);
215#else
216 ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks);
217#endif
218
219 return ret;
220}
221
222#ifdef DEBUG
223#define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
224 if (set_sdrc_timing_regval_ps((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800225 memory_timings->field, \
Tero Kristo20006552009-11-22 10:11:36 -0800226 (rate), #field) < 0) \
227 err = -1;
228
229#else
230#define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
231 if (set_sdrc_timing_regval_ps((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800232 memory_timings->field, (rate)) < 0) \
Tero Kristo20006552009-11-22 10:11:36 -0800233 err = -1;
234#endif
235
Aaro Koskinenfbd208e2010-12-17 15:13:45 -0800236static int sdrc_timings(int id, long rate,
237 const struct sdram_timings *memory_timings)
Tero Kristo20006552009-11-22 10:11:36 -0800238{
239 u32 ticks_per_ms;
240 u32 rfr, l;
241 u32 actim_ctrla = 0, actim_ctrlb = 0;
242 u32 rfr_ctrl;
243 int err = 0;
244 long l3_rate = rate / 1000;
245
246 SDRC_SET_ONE_PS(&actim_ctrla, 0, 4, tDAL, l3_rate);
247 SDRC_SET_ONE_PS(&actim_ctrla, 6, 8, tDPL, l3_rate);
248 SDRC_SET_ONE_PS(&actim_ctrla, 9, 11, tRRD, l3_rate);
249 SDRC_SET_ONE_PS(&actim_ctrla, 12, 14, tRCD, l3_rate);
250 SDRC_SET_ONE_PS(&actim_ctrla, 15, 17, tRP, l3_rate);
251 SDRC_SET_ONE_PS(&actim_ctrla, 18, 21, tRAS, l3_rate);
252 SDRC_SET_ONE_PS(&actim_ctrla, 22, 26, tRC, l3_rate);
253 SDRC_SET_ONE_PS(&actim_ctrla, 27, 31, tRFC, l3_rate);
254
255 SDRC_SET_ONE_PS(&actim_ctrlb, 0, 7, tXSR, l3_rate);
256
257 SDRC_SET_ONE(&actim_ctrlb, 8, 10, tXP, l3_rate);
258 SDRC_SET_ONE(&actim_ctrlb, 12, 14, tCKE, l3_rate);
259 SDRC_SET_ONE(&actim_ctrlb, 16, 17, tWTR, l3_rate);
260
261 ticks_per_ms = l3_rate;
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800262 rfr = memory_timings[0].tREF * ticks_per_ms / 1000000;
Tero Kristo20006552009-11-22 10:11:36 -0800263 if (rfr > 65535 + 50)
264 rfr = 65535;
265 else
266 rfr -= 50;
267
268#ifdef DEBUG
269 printk(KERN_INFO "SDRC tREF: %i ticks\n", rfr);
270#endif
271
272 l = rfr << 8;
273 rfr_ctrl = l | 0x1; /* autorefresh, reload counter with 1xARCV */
274
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800275 nokia_sdrc_params[id].rate = rate;
276 nokia_sdrc_params[id].actim_ctrla = actim_ctrla;
277 nokia_sdrc_params[id].actim_ctrlb = actim_ctrlb;
278 nokia_sdrc_params[id].rfr_ctrl = rfr_ctrl;
279 nokia_sdrc_params[id].mr = 0x32;
Tero Kristo20006552009-11-22 10:11:36 -0800280
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800281 nokia_sdrc_params[id + 1].rate = 0;
Tero Kristo20006552009-11-22 10:11:36 -0800282
283 return err;
284}
285
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800286struct omap_sdrc_params *nokia_get_sdram_timings(void)
Tero Kristo20006552009-11-22 10:11:36 -0800287{
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800288 int err = 0;
289 int i;
Tero Kristo20006552009-11-22 10:11:36 -0800290
Aaro Koskinen2b1af872010-12-17 15:13:46 -0800291 for (i = 0; i < ARRAY_SIZE(nokia_timings); i++) {
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800292 err |= sdrc_timings(i, nokia_timings[i].rate,
293 nokia_timings[i].data);
Aaro Koskinen2b1af872010-12-17 15:13:46 -0800294 if (err)
295 pr_err("%s: error with rate %ld: %d\n", __func__,
296 nokia_timings[i].rate, err);
297 }
Tero Kristo20006552009-11-22 10:11:36 -0800298
Aaro Koskinen2b1af872010-12-17 15:13:46 -0800299 return err ? NULL : nokia_sdrc_params;
Tero Kristo20006552009-11-22 10:11:36 -0800300}
301