sdram-nokia.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * SDRC register values for Nokia boards
  3. *
  4. * Copyright (C) 2008, 2010-2011 Nokia Corporation
  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. #include <linux/kernel.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <plat/io.h>
  19. #include "common.h"
  20. #include <plat/clock.h>
  21. #include <plat/sdrc.h>
  22. #include "sdram-nokia.h"
  23. /* In picoseconds, except for tREF (ns), tXP, tCKE, tWTR (clks) */
  24. struct sdram_timings {
  25. u32 casl;
  26. u32 tDAL;
  27. u32 tDPL;
  28. u32 tRRD;
  29. u32 tRCD;
  30. u32 tRP;
  31. u32 tRAS;
  32. u32 tRC;
  33. u32 tRFC;
  34. u32 tXSR;
  35. u32 tREF; /* in ns */
  36. u32 tXP;
  37. u32 tCKE;
  38. u32 tWTR;
  39. };
  40. static const struct sdram_timings nokia_97dot6mhz_timings[] = {
  41. {
  42. .casl = 3,
  43. .tDAL = 30725,
  44. .tDPL = 15362,
  45. .tRRD = 10241,
  46. .tRCD = 20483,
  47. .tRP = 15362,
  48. .tRAS = 40967,
  49. .tRC = 56330,
  50. .tRFC = 138266,
  51. .tXSR = 204839,
  52. .tREF = 7798,
  53. .tXP = 2,
  54. .tCKE = 4,
  55. .tWTR = 2,
  56. },
  57. };
  58. static const struct sdram_timings nokia_166mhz_timings[] = {
  59. {
  60. .casl = 3,
  61. .tDAL = 33000,
  62. .tDPL = 15000,
  63. .tRRD = 12000,
  64. .tRCD = 22500,
  65. .tRP = 18000,
  66. .tRAS = 42000,
  67. .tRC = 66000,
  68. .tRFC = 138000,
  69. .tXSR = 200000,
  70. .tREF = 7800,
  71. .tXP = 2,
  72. .tCKE = 2,
  73. .tWTR = 2
  74. },
  75. };
  76. static const struct sdram_timings nokia_195dot2mhz_timings[] = {
  77. {
  78. .casl = 3,
  79. .tDAL = 30725,
  80. .tDPL = 15362,
  81. .tRRD = 10241,
  82. .tRCD = 20483,
  83. .tRP = 15362,
  84. .tRAS = 40967,
  85. .tRC = 56330,
  86. .tRFC = 138266,
  87. .tXSR = 204839,
  88. .tREF = 7752,
  89. .tXP = 2,
  90. .tCKE = 4,
  91. .tWTR = 2,
  92. },
  93. };
  94. static const struct sdram_timings nokia_200mhz_timings[] = {
  95. {
  96. .casl = 3,
  97. .tDAL = 30000,
  98. .tDPL = 15000,
  99. .tRRD = 10000,
  100. .tRCD = 20000,
  101. .tRP = 15000,
  102. .tRAS = 40000,
  103. .tRC = 55000,
  104. .tRFC = 140000,
  105. .tXSR = 200000,
  106. .tREF = 7800,
  107. .tXP = 2,
  108. .tCKE = 4,
  109. .tWTR = 2
  110. },
  111. };
  112. static const struct {
  113. long rate;
  114. struct sdram_timings const *data;
  115. } nokia_timings[] = {
  116. { 83000000, nokia_166mhz_timings },
  117. { 97600000, nokia_97dot6mhz_timings },
  118. { 100000000, nokia_200mhz_timings },
  119. { 166000000, nokia_166mhz_timings },
  120. { 195200000, nokia_195dot2mhz_timings },
  121. { 200000000, nokia_200mhz_timings },
  122. };
  123. static struct omap_sdrc_params nokia_sdrc_params[ARRAY_SIZE(nokia_timings) + 1];
  124. static unsigned long sdrc_get_fclk_period(long rate)
  125. {
  126. /* In picoseconds */
  127. return 1000000000 / rate;
  128. }
  129. static unsigned int sdrc_ps_to_ticks(unsigned int time_ps, long rate)
  130. {
  131. unsigned long tick_ps;
  132. /* Calculate in picosecs to yield more exact results */
  133. tick_ps = sdrc_get_fclk_period(rate);
  134. return (time_ps + tick_ps - 1) / tick_ps;
  135. }
  136. #undef DEBUG
  137. #ifdef DEBUG
  138. static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
  139. int ticks, long rate, const char *name)
  140. #else
  141. static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
  142. int ticks)
  143. #endif
  144. {
  145. int mask, nr_bits;
  146. nr_bits = end_bit - st_bit + 1;
  147. if (ticks >= 1 << nr_bits)
  148. return -1;
  149. mask = (1 << nr_bits) - 1;
  150. *regval &= ~(mask << st_bit);
  151. *regval |= ticks << st_bit;
  152. #ifdef DEBUG
  153. printk(KERN_INFO "SDRC %s: %i ticks %i ns\n", name, ticks,
  154. (unsigned int)sdrc_get_fclk_period(rate) * ticks /
  155. 1000);
  156. #endif
  157. return 0;
  158. }
  159. #ifdef DEBUG
  160. #define SDRC_SET_ONE(reg, st, end, field, rate) \
  161. if (set_sdrc_timing_regval((reg), (st), (end), \
  162. memory_timings->field, (rate), #field) < 0) \
  163. err = -1;
  164. #else
  165. #define SDRC_SET_ONE(reg, st, end, field, rate) \
  166. if (set_sdrc_timing_regval((reg), (st), (end), \
  167. memory_timings->field) < 0) \
  168. err = -1;
  169. #endif
  170. #ifdef DEBUG
  171. static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
  172. int time, long rate, const char *name)
  173. #else
  174. static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
  175. int time, long rate)
  176. #endif
  177. {
  178. int ticks, ret;
  179. ret = 0;
  180. if (time == 0)
  181. ticks = 0;
  182. else
  183. ticks = sdrc_ps_to_ticks(time, rate);
  184. #ifdef DEBUG
  185. ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks,
  186. rate, name);
  187. #else
  188. ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks);
  189. #endif
  190. return ret;
  191. }
  192. #ifdef DEBUG
  193. #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
  194. if (set_sdrc_timing_regval_ps((reg), (st), (end), \
  195. memory_timings->field, \
  196. (rate), #field) < 0) \
  197. err = -1;
  198. #else
  199. #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
  200. if (set_sdrc_timing_regval_ps((reg), (st), (end), \
  201. memory_timings->field, (rate)) < 0) \
  202. err = -1;
  203. #endif
  204. static int sdrc_timings(int id, long rate,
  205. const struct sdram_timings *memory_timings)
  206. {
  207. u32 ticks_per_ms;
  208. u32 rfr, l;
  209. u32 actim_ctrla = 0, actim_ctrlb = 0;
  210. u32 rfr_ctrl;
  211. int err = 0;
  212. long l3_rate = rate / 1000;
  213. SDRC_SET_ONE_PS(&actim_ctrla, 0, 4, tDAL, l3_rate);
  214. SDRC_SET_ONE_PS(&actim_ctrla, 6, 8, tDPL, l3_rate);
  215. SDRC_SET_ONE_PS(&actim_ctrla, 9, 11, tRRD, l3_rate);
  216. SDRC_SET_ONE_PS(&actim_ctrla, 12, 14, tRCD, l3_rate);
  217. SDRC_SET_ONE_PS(&actim_ctrla, 15, 17, tRP, l3_rate);
  218. SDRC_SET_ONE_PS(&actim_ctrla, 18, 21, tRAS, l3_rate);
  219. SDRC_SET_ONE_PS(&actim_ctrla, 22, 26, tRC, l3_rate);
  220. SDRC_SET_ONE_PS(&actim_ctrla, 27, 31, tRFC, l3_rate);
  221. SDRC_SET_ONE_PS(&actim_ctrlb, 0, 7, tXSR, l3_rate);
  222. SDRC_SET_ONE(&actim_ctrlb, 8, 10, tXP, l3_rate);
  223. SDRC_SET_ONE(&actim_ctrlb, 12, 14, tCKE, l3_rate);
  224. SDRC_SET_ONE(&actim_ctrlb, 16, 17, tWTR, l3_rate);
  225. ticks_per_ms = l3_rate;
  226. rfr = memory_timings[0].tREF * ticks_per_ms / 1000000;
  227. if (rfr > 65535 + 50)
  228. rfr = 65535;
  229. else
  230. rfr -= 50;
  231. #ifdef DEBUG
  232. printk(KERN_INFO "SDRC tREF: %i ticks\n", rfr);
  233. #endif
  234. l = rfr << 8;
  235. rfr_ctrl = l | 0x1; /* autorefresh, reload counter with 1xARCV */
  236. nokia_sdrc_params[id].rate = rate;
  237. nokia_sdrc_params[id].actim_ctrla = actim_ctrla;
  238. nokia_sdrc_params[id].actim_ctrlb = actim_ctrlb;
  239. nokia_sdrc_params[id].rfr_ctrl = rfr_ctrl;
  240. nokia_sdrc_params[id].mr = 0x32;
  241. nokia_sdrc_params[id + 1].rate = 0;
  242. return err;
  243. }
  244. struct omap_sdrc_params *nokia_get_sdram_timings(void)
  245. {
  246. int err = 0;
  247. int i;
  248. for (i = 0; i < ARRAY_SIZE(nokia_timings); i++) {
  249. err |= sdrc_timings(i, nokia_timings[i].rate,
  250. nokia_timings[i].data);
  251. if (err)
  252. pr_err("%s: error with rate %ld: %d\n", __func__,
  253. nokia_timings[i].rate, err);
  254. }
  255. return err ? NULL : nokia_sdrc_params;
  256. }