sdram-nokia.c 6.3 KB

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