sdram-nokia.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * SDRC register values for Nokia boards
  3. *
  4. * Copyright (C) 2008, 2010 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 <plat/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 {
  95. long rate;
  96. struct sdram_timings const *data;
  97. } nokia_timings[] = {
  98. { 83000000, nokia_166mhz_timings },
  99. { 97600000, nokia_97dot6mhz_timings },
  100. { 166000000, nokia_166mhz_timings },
  101. { 195200000, nokia_195dot2mhz_timings },
  102. };
  103. static struct omap_sdrc_params nokia_sdrc_params[ARRAY_SIZE(nokia_timings) + 1];
  104. static unsigned long sdrc_get_fclk_period(long rate)
  105. {
  106. /* In picoseconds */
  107. return 1000000000 / rate;
  108. }
  109. static unsigned int sdrc_ps_to_ticks(unsigned int time_ps, long rate)
  110. {
  111. unsigned long tick_ps;
  112. /* Calculate in picosecs to yield more exact results */
  113. tick_ps = sdrc_get_fclk_period(rate);
  114. return (time_ps + tick_ps - 1) / tick_ps;
  115. }
  116. #undef DEBUG
  117. #ifdef DEBUG
  118. static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
  119. int ticks, long rate, const char *name)
  120. #else
  121. static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
  122. int ticks)
  123. #endif
  124. {
  125. int mask, nr_bits;
  126. nr_bits = end_bit - st_bit + 1;
  127. if (ticks >= 1 << nr_bits)
  128. return -1;
  129. mask = (1 << nr_bits) - 1;
  130. *regval &= ~(mask << st_bit);
  131. *regval |= ticks << st_bit;
  132. #ifdef DEBUG
  133. printk(KERN_INFO "SDRC %s: %i ticks %i ns\n", name, ticks,
  134. (unsigned int)sdrc_get_fclk_period(rate) * ticks /
  135. 1000);
  136. #endif
  137. return 0;
  138. }
  139. #ifdef DEBUG
  140. #define SDRC_SET_ONE(reg, st, end, field, rate) \
  141. if (set_sdrc_timing_regval((reg), (st), (end), \
  142. memory_timings->field, (rate), #field) < 0) \
  143. err = -1;
  144. #else
  145. #define SDRC_SET_ONE(reg, st, end, field, rate) \
  146. if (set_sdrc_timing_regval((reg), (st), (end), \
  147. memory_timings->field) < 0) \
  148. err = -1;
  149. #endif
  150. #ifdef DEBUG
  151. static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
  152. int time, long rate, const char *name)
  153. #else
  154. static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
  155. int time, long rate)
  156. #endif
  157. {
  158. int ticks, ret;
  159. ret = 0;
  160. if (time == 0)
  161. ticks = 0;
  162. else
  163. ticks = sdrc_ps_to_ticks(time, rate);
  164. #ifdef DEBUG
  165. ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks,
  166. rate, name);
  167. #else
  168. ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks);
  169. #endif
  170. return ret;
  171. }
  172. #ifdef DEBUG
  173. #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
  174. if (set_sdrc_timing_regval_ps((reg), (st), (end), \
  175. memory_timings->field, \
  176. (rate), #field) < 0) \
  177. err = -1;
  178. #else
  179. #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
  180. if (set_sdrc_timing_regval_ps((reg), (st), (end), \
  181. memory_timings->field, (rate)) < 0) \
  182. err = -1;
  183. #endif
  184. static int sdrc_timings(int id, long rate,
  185. const struct sdram_timings *memory_timings)
  186. {
  187. u32 ticks_per_ms;
  188. u32 rfr, l;
  189. u32 actim_ctrla = 0, actim_ctrlb = 0;
  190. u32 rfr_ctrl;
  191. int err = 0;
  192. long l3_rate = rate / 1000;
  193. SDRC_SET_ONE_PS(&actim_ctrla, 0, 4, tDAL, l3_rate);
  194. SDRC_SET_ONE_PS(&actim_ctrla, 6, 8, tDPL, l3_rate);
  195. SDRC_SET_ONE_PS(&actim_ctrla, 9, 11, tRRD, l3_rate);
  196. SDRC_SET_ONE_PS(&actim_ctrla, 12, 14, tRCD, l3_rate);
  197. SDRC_SET_ONE_PS(&actim_ctrla, 15, 17, tRP, l3_rate);
  198. SDRC_SET_ONE_PS(&actim_ctrla, 18, 21, tRAS, l3_rate);
  199. SDRC_SET_ONE_PS(&actim_ctrla, 22, 26, tRC, l3_rate);
  200. SDRC_SET_ONE_PS(&actim_ctrla, 27, 31, tRFC, l3_rate);
  201. SDRC_SET_ONE_PS(&actim_ctrlb, 0, 7, tXSR, l3_rate);
  202. SDRC_SET_ONE(&actim_ctrlb, 8, 10, tXP, l3_rate);
  203. SDRC_SET_ONE(&actim_ctrlb, 12, 14, tCKE, l3_rate);
  204. SDRC_SET_ONE(&actim_ctrlb, 16, 17, tWTR, l3_rate);
  205. ticks_per_ms = l3_rate;
  206. rfr = memory_timings[0].tREF * ticks_per_ms / 1000000;
  207. if (rfr > 65535 + 50)
  208. rfr = 65535;
  209. else
  210. rfr -= 50;
  211. #ifdef DEBUG
  212. printk(KERN_INFO "SDRC tREF: %i ticks\n", rfr);
  213. #endif
  214. l = rfr << 8;
  215. rfr_ctrl = l | 0x1; /* autorefresh, reload counter with 1xARCV */
  216. nokia_sdrc_params[id].rate = rate;
  217. nokia_sdrc_params[id].actim_ctrla = actim_ctrla;
  218. nokia_sdrc_params[id].actim_ctrlb = actim_ctrlb;
  219. nokia_sdrc_params[id].rfr_ctrl = rfr_ctrl;
  220. nokia_sdrc_params[id].mr = 0x32;
  221. nokia_sdrc_params[id + 1].rate = 0;
  222. return err;
  223. }
  224. struct omap_sdrc_params *nokia_get_sdram_timings(void)
  225. {
  226. int err = 0;
  227. int i;
  228. for (i = 0; i < ARRAY_SIZE(nokia_timings); i++) {
  229. err |= sdrc_timings(i, nokia_timings[i].rate,
  230. nokia_timings[i].data);
  231. if (err)
  232. pr_err("%s: error with rate %ld: %d\n", __func__,
  233. nokia_timings[i].rate, err);
  234. }
  235. return err ? NULL : nokia_sdrc_params;
  236. }