blob: baf83ecdada1e2650d3155df00bb3101417c6316 [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 *
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -08004 * Copyright (C) 2008, 2010 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
20#include <plat/io.h>
21#include <plat/common.h>
22#include <plat/clock.h>
23#include <plat/sdrc.h>
24
Aaro Koskinenfcd8d8462010-12-17 15:13:44 -080025#include "sdram-nokia.h"
Tero Kristo20006552009-11-22 10:11:36 -080026
27/* In picoseconds, except for tREF (ns), tXP, tCKE, tWTR (clks) */
28struct sdram_timings {
29 u32 casl;
30 u32 tDAL;
31 u32 tDPL;
32 u32 tRRD;
33 u32 tRCD;
34 u32 tRP;
35 u32 tRAS;
36 u32 tRC;
37 u32 tRFC;
38 u32 tXSR;
39
40 u32 tREF; /* in ns */
41
42 u32 tXP;
43 u32 tCKE;
44 u32 tWTR;
45};
46
Aaro Koskinenfbd208e2010-12-17 15:13:45 -080047static const struct sdram_timings nokia_166mhz_timings[] = {
Tero Kristo20006552009-11-22 10:11:36 -080048 {
49 .casl = 3,
50 .tDAL = 33000,
51 .tDPL = 15000,
52 .tRRD = 12000,
53 .tRCD = 22500,
54 .tRP = 18000,
55 .tRAS = 42000,
56 .tRC = 66000,
57 .tRFC = 138000,
58 .tXSR = 200000,
59
60 .tREF = 7800,
61
62 .tXP = 2,
63 .tCKE = 2,
64 .tWTR = 2
65 },
66};
67
Aaro Koskinene5f5b542010-12-17 15:13:45 -080068static const struct {
69 long rate;
70 struct sdram_timings const *data;
71} nokia_timings[] = {
Aaro Koskinene5f5b542010-12-17 15:13:45 -080072 { 83000000, nokia_166mhz_timings },
73 { 166000000, nokia_166mhz_timings },
74};
75static struct omap_sdrc_params nokia_sdrc_params[ARRAY_SIZE(nokia_timings) + 1];
76
Tero Kristo20006552009-11-22 10:11:36 -080077static unsigned long sdrc_get_fclk_period(long rate)
78{
79 /* In picoseconds */
80 return 1000000000 / rate;
81}
82
83static unsigned int sdrc_ps_to_ticks(unsigned int time_ps, long rate)
84{
85 unsigned long tick_ps;
86
87 /* Calculate in picosecs to yield more exact results */
88 tick_ps = sdrc_get_fclk_period(rate);
89
90 return (time_ps + tick_ps - 1) / tick_ps;
91}
92#undef DEBUG
93#ifdef DEBUG
94static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
95 int ticks, long rate, const char *name)
96#else
97static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
98 int ticks)
99#endif
100{
101 int mask, nr_bits;
102
103 nr_bits = end_bit - st_bit + 1;
104 if (ticks >= 1 << nr_bits)
105 return -1;
106 mask = (1 << nr_bits) - 1;
107 *regval &= ~(mask << st_bit);
108 *regval |= ticks << st_bit;
109#ifdef DEBUG
110 printk(KERN_INFO "SDRC %s: %i ticks %i ns\n", name, ticks,
111 (unsigned int)sdrc_get_fclk_period(rate) * ticks /
112 1000);
113#endif
114
115 return 0;
116}
117
118#ifdef DEBUG
119#define SDRC_SET_ONE(reg, st, end, field, rate) \
120 if (set_sdrc_timing_regval((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800121 memory_timings->field, (rate), #field) < 0) \
Tero Kristo20006552009-11-22 10:11:36 -0800122 err = -1;
123#else
124#define SDRC_SET_ONE(reg, st, end, field, rate) \
125 if (set_sdrc_timing_regval((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800126 memory_timings->field) < 0) \
Tero Kristo20006552009-11-22 10:11:36 -0800127 err = -1;
128#endif
129
130#ifdef DEBUG
131static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
132 int time, long rate, const char *name)
133#else
134static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
135 int time, long rate)
136#endif
137{
138 int ticks, ret;
139 ret = 0;
140
141 if (time == 0)
142 ticks = 0;
143 else
144 ticks = sdrc_ps_to_ticks(time, rate);
145
146#ifdef DEBUG
147 ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks,
148 rate, name);
149#else
150 ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks);
151#endif
152
153 return ret;
154}
155
156#ifdef DEBUG
157#define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
158 if (set_sdrc_timing_regval_ps((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800159 memory_timings->field, \
Tero Kristo20006552009-11-22 10:11:36 -0800160 (rate), #field) < 0) \
161 err = -1;
162
163#else
164#define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
165 if (set_sdrc_timing_regval_ps((reg), (st), (end), \
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800166 memory_timings->field, (rate)) < 0) \
Tero Kristo20006552009-11-22 10:11:36 -0800167 err = -1;
168#endif
169
Aaro Koskinenfbd208e2010-12-17 15:13:45 -0800170static int sdrc_timings(int id, long rate,
171 const struct sdram_timings *memory_timings)
Tero Kristo20006552009-11-22 10:11:36 -0800172{
173 u32 ticks_per_ms;
174 u32 rfr, l;
175 u32 actim_ctrla = 0, actim_ctrlb = 0;
176 u32 rfr_ctrl;
177 int err = 0;
178 long l3_rate = rate / 1000;
179
180 SDRC_SET_ONE_PS(&actim_ctrla, 0, 4, tDAL, l3_rate);
181 SDRC_SET_ONE_PS(&actim_ctrla, 6, 8, tDPL, l3_rate);
182 SDRC_SET_ONE_PS(&actim_ctrla, 9, 11, tRRD, l3_rate);
183 SDRC_SET_ONE_PS(&actim_ctrla, 12, 14, tRCD, l3_rate);
184 SDRC_SET_ONE_PS(&actim_ctrla, 15, 17, tRP, l3_rate);
185 SDRC_SET_ONE_PS(&actim_ctrla, 18, 21, tRAS, l3_rate);
186 SDRC_SET_ONE_PS(&actim_ctrla, 22, 26, tRC, l3_rate);
187 SDRC_SET_ONE_PS(&actim_ctrla, 27, 31, tRFC, l3_rate);
188
189 SDRC_SET_ONE_PS(&actim_ctrlb, 0, 7, tXSR, l3_rate);
190
191 SDRC_SET_ONE(&actim_ctrlb, 8, 10, tXP, l3_rate);
192 SDRC_SET_ONE(&actim_ctrlb, 12, 14, tCKE, l3_rate);
193 SDRC_SET_ONE(&actim_ctrlb, 16, 17, tWTR, l3_rate);
194
195 ticks_per_ms = l3_rate;
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800196 rfr = memory_timings[0].tREF * ticks_per_ms / 1000000;
Tero Kristo20006552009-11-22 10:11:36 -0800197 if (rfr > 65535 + 50)
198 rfr = 65535;
199 else
200 rfr -= 50;
201
202#ifdef DEBUG
203 printk(KERN_INFO "SDRC tREF: %i ticks\n", rfr);
204#endif
205
206 l = rfr << 8;
207 rfr_ctrl = l | 0x1; /* autorefresh, reload counter with 1xARCV */
208
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800209 nokia_sdrc_params[id].rate = rate;
210 nokia_sdrc_params[id].actim_ctrla = actim_ctrla;
211 nokia_sdrc_params[id].actim_ctrlb = actim_ctrlb;
212 nokia_sdrc_params[id].rfr_ctrl = rfr_ctrl;
213 nokia_sdrc_params[id].mr = 0x32;
Tero Kristo20006552009-11-22 10:11:36 -0800214
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800215 nokia_sdrc_params[id + 1].rate = 0;
Tero Kristo20006552009-11-22 10:11:36 -0800216
217 return err;
218}
219
Aaro Koskinen6c3bc4e2010-12-17 15:13:44 -0800220struct omap_sdrc_params *nokia_get_sdram_timings(void)
Tero Kristo20006552009-11-22 10:11:36 -0800221{
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800222 int err = 0;
223 int i;
Tero Kristo20006552009-11-22 10:11:36 -0800224
Aaro Koskinen2b1af872010-12-17 15:13:46 -0800225 for (i = 0; i < ARRAY_SIZE(nokia_timings); i++) {
Aaro Koskinene5f5b542010-12-17 15:13:45 -0800226 err |= sdrc_timings(i, nokia_timings[i].rate,
227 nokia_timings[i].data);
Aaro Koskinen2b1af872010-12-17 15:13:46 -0800228 if (err)
229 pr_err("%s: error with rate %ld: %d\n", __func__,
230 nokia_timings[i].rate, err);
231 }
Tero Kristo20006552009-11-22 10:11:36 -0800232
Aaro Koskinen2b1af872010-12-17 15:13:46 -0800233 return err ? NULL : nokia_sdrc_params;
Tero Kristo20006552009-11-22 10:11:36 -0800234}
235