Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 1 | /* |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 2 | * SDRC register values for Nokia boards |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 3 | * |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 4 | * Copyright (C) 2008, 2010 Nokia Corporation |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 5 | * |
| 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 Koskinen | fcd8d84 | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 25 | #include "sdram-nokia.h" |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 26 | |
| 27 | /* In picoseconds, except for tREF (ns), tXP, tCKE, tWTR (clks) */ |
| 28 | struct 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 Koskinen | fbd208e | 2010-12-17 15:13:45 -0800 | [diff] [blame] | 47 | static const struct sdram_timings nokia_166mhz_timings[] = { |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 48 | { |
| 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 Koskinen | e5f5b54 | 2010-12-17 15:13:45 -0800 | [diff] [blame^] | 68 | static const struct { |
| 69 | long rate; |
| 70 | struct sdram_timings const *data; |
| 71 | } nokia_timings[] = { |
| 72 | { 41500000, nokia_166mhz_timings }, |
| 73 | { 83000000, nokia_166mhz_timings }, |
| 74 | { 166000000, nokia_166mhz_timings }, |
| 75 | }; |
| 76 | static struct omap_sdrc_params nokia_sdrc_params[ARRAY_SIZE(nokia_timings) + 1]; |
| 77 | |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 78 | static unsigned long sdrc_get_fclk_period(long rate) |
| 79 | { |
| 80 | /* In picoseconds */ |
| 81 | return 1000000000 / rate; |
| 82 | } |
| 83 | |
| 84 | static unsigned int sdrc_ps_to_ticks(unsigned int time_ps, long rate) |
| 85 | { |
| 86 | unsigned long tick_ps; |
| 87 | |
| 88 | /* Calculate in picosecs to yield more exact results */ |
| 89 | tick_ps = sdrc_get_fclk_period(rate); |
| 90 | |
| 91 | return (time_ps + tick_ps - 1) / tick_ps; |
| 92 | } |
| 93 | #undef DEBUG |
| 94 | #ifdef DEBUG |
| 95 | static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit, |
| 96 | int ticks, long rate, const char *name) |
| 97 | #else |
| 98 | static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit, |
| 99 | int ticks) |
| 100 | #endif |
| 101 | { |
| 102 | int mask, nr_bits; |
| 103 | |
| 104 | nr_bits = end_bit - st_bit + 1; |
| 105 | if (ticks >= 1 << nr_bits) |
| 106 | return -1; |
| 107 | mask = (1 << nr_bits) - 1; |
| 108 | *regval &= ~(mask << st_bit); |
| 109 | *regval |= ticks << st_bit; |
| 110 | #ifdef DEBUG |
| 111 | printk(KERN_INFO "SDRC %s: %i ticks %i ns\n", name, ticks, |
| 112 | (unsigned int)sdrc_get_fclk_period(rate) * ticks / |
| 113 | 1000); |
| 114 | #endif |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | #ifdef DEBUG |
| 120 | #define SDRC_SET_ONE(reg, st, end, field, rate) \ |
| 121 | if (set_sdrc_timing_regval((reg), (st), (end), \ |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 122 | memory_timings->field, (rate), #field) < 0) \ |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 123 | err = -1; |
| 124 | #else |
| 125 | #define SDRC_SET_ONE(reg, st, end, field, rate) \ |
| 126 | if (set_sdrc_timing_regval((reg), (st), (end), \ |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 127 | memory_timings->field) < 0) \ |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 128 | err = -1; |
| 129 | #endif |
| 130 | |
| 131 | #ifdef DEBUG |
| 132 | static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit, |
| 133 | int time, long rate, const char *name) |
| 134 | #else |
| 135 | static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit, |
| 136 | int time, long rate) |
| 137 | #endif |
| 138 | { |
| 139 | int ticks, ret; |
| 140 | ret = 0; |
| 141 | |
| 142 | if (time == 0) |
| 143 | ticks = 0; |
| 144 | else |
| 145 | ticks = sdrc_ps_to_ticks(time, rate); |
| 146 | |
| 147 | #ifdef DEBUG |
| 148 | ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks, |
| 149 | rate, name); |
| 150 | #else |
| 151 | ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks); |
| 152 | #endif |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | #ifdef DEBUG |
| 158 | #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \ |
| 159 | if (set_sdrc_timing_regval_ps((reg), (st), (end), \ |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 160 | memory_timings->field, \ |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 161 | (rate), #field) < 0) \ |
| 162 | err = -1; |
| 163 | |
| 164 | #else |
| 165 | #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \ |
| 166 | if (set_sdrc_timing_regval_ps((reg), (st), (end), \ |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 167 | memory_timings->field, (rate)) < 0) \ |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 168 | err = -1; |
| 169 | #endif |
| 170 | |
Aaro Koskinen | fbd208e | 2010-12-17 15:13:45 -0800 | [diff] [blame] | 171 | static int sdrc_timings(int id, long rate, |
| 172 | const struct sdram_timings *memory_timings) |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 173 | { |
| 174 | u32 ticks_per_ms; |
| 175 | u32 rfr, l; |
| 176 | u32 actim_ctrla = 0, actim_ctrlb = 0; |
| 177 | u32 rfr_ctrl; |
| 178 | int err = 0; |
| 179 | long l3_rate = rate / 1000; |
| 180 | |
| 181 | SDRC_SET_ONE_PS(&actim_ctrla, 0, 4, tDAL, l3_rate); |
| 182 | SDRC_SET_ONE_PS(&actim_ctrla, 6, 8, tDPL, l3_rate); |
| 183 | SDRC_SET_ONE_PS(&actim_ctrla, 9, 11, tRRD, l3_rate); |
| 184 | SDRC_SET_ONE_PS(&actim_ctrla, 12, 14, tRCD, l3_rate); |
| 185 | SDRC_SET_ONE_PS(&actim_ctrla, 15, 17, tRP, l3_rate); |
| 186 | SDRC_SET_ONE_PS(&actim_ctrla, 18, 21, tRAS, l3_rate); |
| 187 | SDRC_SET_ONE_PS(&actim_ctrla, 22, 26, tRC, l3_rate); |
| 188 | SDRC_SET_ONE_PS(&actim_ctrla, 27, 31, tRFC, l3_rate); |
| 189 | |
| 190 | SDRC_SET_ONE_PS(&actim_ctrlb, 0, 7, tXSR, l3_rate); |
| 191 | |
| 192 | SDRC_SET_ONE(&actim_ctrlb, 8, 10, tXP, l3_rate); |
| 193 | SDRC_SET_ONE(&actim_ctrlb, 12, 14, tCKE, l3_rate); |
| 194 | SDRC_SET_ONE(&actim_ctrlb, 16, 17, tWTR, l3_rate); |
| 195 | |
| 196 | ticks_per_ms = l3_rate; |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 197 | rfr = memory_timings[0].tREF * ticks_per_ms / 1000000; |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 198 | if (rfr > 65535 + 50) |
| 199 | rfr = 65535; |
| 200 | else |
| 201 | rfr -= 50; |
| 202 | |
| 203 | #ifdef DEBUG |
| 204 | printk(KERN_INFO "SDRC tREF: %i ticks\n", rfr); |
| 205 | #endif |
| 206 | |
| 207 | l = rfr << 8; |
| 208 | rfr_ctrl = l | 0x1; /* autorefresh, reload counter with 1xARCV */ |
| 209 | |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 210 | nokia_sdrc_params[id].rate = rate; |
| 211 | nokia_sdrc_params[id].actim_ctrla = actim_ctrla; |
| 212 | nokia_sdrc_params[id].actim_ctrlb = actim_ctrlb; |
| 213 | nokia_sdrc_params[id].rfr_ctrl = rfr_ctrl; |
| 214 | nokia_sdrc_params[id].mr = 0x32; |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 215 | |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 216 | nokia_sdrc_params[id + 1].rate = 0; |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 217 | |
| 218 | return err; |
| 219 | } |
| 220 | |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 221 | struct omap_sdrc_params *nokia_get_sdram_timings(void) |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 222 | { |
Aaro Koskinen | e5f5b54 | 2010-12-17 15:13:45 -0800 | [diff] [blame^] | 223 | int err = 0; |
| 224 | int i; |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 225 | |
Aaro Koskinen | e5f5b54 | 2010-12-17 15:13:45 -0800 | [diff] [blame^] | 226 | for (i = 0; i < ARRAY_SIZE(nokia_timings); i++) |
| 227 | err |= sdrc_timings(i, nokia_timings[i].rate, |
| 228 | nokia_timings[i].data); |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 229 | |
Aaro Koskinen | 6c3bc4e | 2010-12-17 15:13:44 -0800 | [diff] [blame] | 230 | return &nokia_sdrc_params[0]; |
Tero Kristo | 2000655 | 2009-11-22 10:11:36 -0800 | [diff] [blame] | 231 | } |
| 232 | |