sdram-nokia.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_166mhz_timings[] = {
  41. {
  42. .casl = 3,
  43. .tDAL = 33000,
  44. .tDPL = 15000,
  45. .tRRD = 12000,
  46. .tRCD = 22500,
  47. .tRP = 18000,
  48. .tRAS = 42000,
  49. .tRC = 66000,
  50. .tRFC = 138000,
  51. .tXSR = 200000,
  52. .tREF = 7800,
  53. .tXP = 2,
  54. .tCKE = 2,
  55. .tWTR = 2
  56. },
  57. };
  58. static const struct {
  59. long rate;
  60. struct sdram_timings const *data;
  61. } nokia_timings[] = {
  62. { 83000000, nokia_166mhz_timings },
  63. { 166000000, nokia_166mhz_timings },
  64. };
  65. static struct omap_sdrc_params nokia_sdrc_params[ARRAY_SIZE(nokia_timings) + 1];
  66. static unsigned long sdrc_get_fclk_period(long rate)
  67. {
  68. /* In picoseconds */
  69. return 1000000000 / rate;
  70. }
  71. static unsigned int sdrc_ps_to_ticks(unsigned int time_ps, long rate)
  72. {
  73. unsigned long tick_ps;
  74. /* Calculate in picosecs to yield more exact results */
  75. tick_ps = sdrc_get_fclk_period(rate);
  76. return (time_ps + tick_ps - 1) / tick_ps;
  77. }
  78. #undef DEBUG
  79. #ifdef DEBUG
  80. static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
  81. int ticks, long rate, const char *name)
  82. #else
  83. static int set_sdrc_timing_regval(u32 *regval, int st_bit, int end_bit,
  84. int ticks)
  85. #endif
  86. {
  87. int mask, nr_bits;
  88. nr_bits = end_bit - st_bit + 1;
  89. if (ticks >= 1 << nr_bits)
  90. return -1;
  91. mask = (1 << nr_bits) - 1;
  92. *regval &= ~(mask << st_bit);
  93. *regval |= ticks << st_bit;
  94. #ifdef DEBUG
  95. printk(KERN_INFO "SDRC %s: %i ticks %i ns\n", name, ticks,
  96. (unsigned int)sdrc_get_fclk_period(rate) * ticks /
  97. 1000);
  98. #endif
  99. return 0;
  100. }
  101. #ifdef DEBUG
  102. #define SDRC_SET_ONE(reg, st, end, field, rate) \
  103. if (set_sdrc_timing_regval((reg), (st), (end), \
  104. memory_timings->field, (rate), #field) < 0) \
  105. err = -1;
  106. #else
  107. #define SDRC_SET_ONE(reg, st, end, field, rate) \
  108. if (set_sdrc_timing_regval((reg), (st), (end), \
  109. memory_timings->field) < 0) \
  110. err = -1;
  111. #endif
  112. #ifdef DEBUG
  113. static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
  114. int time, long rate, const char *name)
  115. #else
  116. static int set_sdrc_timing_regval_ps(u32 *regval, int st_bit, int end_bit,
  117. int time, long rate)
  118. #endif
  119. {
  120. int ticks, ret;
  121. ret = 0;
  122. if (time == 0)
  123. ticks = 0;
  124. else
  125. ticks = sdrc_ps_to_ticks(time, rate);
  126. #ifdef DEBUG
  127. ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks,
  128. rate, name);
  129. #else
  130. ret = set_sdrc_timing_regval(regval, st_bit, end_bit, ticks);
  131. #endif
  132. return ret;
  133. }
  134. #ifdef DEBUG
  135. #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
  136. if (set_sdrc_timing_regval_ps((reg), (st), (end), \
  137. memory_timings->field, \
  138. (rate), #field) < 0) \
  139. err = -1;
  140. #else
  141. #define SDRC_SET_ONE_PS(reg, st, end, field, rate) \
  142. if (set_sdrc_timing_regval_ps((reg), (st), (end), \
  143. memory_timings->field, (rate)) < 0) \
  144. err = -1;
  145. #endif
  146. static int sdrc_timings(int id, long rate,
  147. const struct sdram_timings *memory_timings)
  148. {
  149. u32 ticks_per_ms;
  150. u32 rfr, l;
  151. u32 actim_ctrla = 0, actim_ctrlb = 0;
  152. u32 rfr_ctrl;
  153. int err = 0;
  154. long l3_rate = rate / 1000;
  155. SDRC_SET_ONE_PS(&actim_ctrla, 0, 4, tDAL, l3_rate);
  156. SDRC_SET_ONE_PS(&actim_ctrla, 6, 8, tDPL, l3_rate);
  157. SDRC_SET_ONE_PS(&actim_ctrla, 9, 11, tRRD, l3_rate);
  158. SDRC_SET_ONE_PS(&actim_ctrla, 12, 14, tRCD, l3_rate);
  159. SDRC_SET_ONE_PS(&actim_ctrla, 15, 17, tRP, l3_rate);
  160. SDRC_SET_ONE_PS(&actim_ctrla, 18, 21, tRAS, l3_rate);
  161. SDRC_SET_ONE_PS(&actim_ctrla, 22, 26, tRC, l3_rate);
  162. SDRC_SET_ONE_PS(&actim_ctrla, 27, 31, tRFC, l3_rate);
  163. SDRC_SET_ONE_PS(&actim_ctrlb, 0, 7, tXSR, l3_rate);
  164. SDRC_SET_ONE(&actim_ctrlb, 8, 10, tXP, l3_rate);
  165. SDRC_SET_ONE(&actim_ctrlb, 12, 14, tCKE, l3_rate);
  166. SDRC_SET_ONE(&actim_ctrlb, 16, 17, tWTR, l3_rate);
  167. ticks_per_ms = l3_rate;
  168. rfr = memory_timings[0].tREF * ticks_per_ms / 1000000;
  169. if (rfr > 65535 + 50)
  170. rfr = 65535;
  171. else
  172. rfr -= 50;
  173. #ifdef DEBUG
  174. printk(KERN_INFO "SDRC tREF: %i ticks\n", rfr);
  175. #endif
  176. l = rfr << 8;
  177. rfr_ctrl = l | 0x1; /* autorefresh, reload counter with 1xARCV */
  178. nokia_sdrc_params[id].rate = rate;
  179. nokia_sdrc_params[id].actim_ctrla = actim_ctrla;
  180. nokia_sdrc_params[id].actim_ctrlb = actim_ctrlb;
  181. nokia_sdrc_params[id].rfr_ctrl = rfr_ctrl;
  182. nokia_sdrc_params[id].mr = 0x32;
  183. nokia_sdrc_params[id + 1].rate = 0;
  184. return err;
  185. }
  186. struct omap_sdrc_params *nokia_get_sdram_timings(void)
  187. {
  188. int err = 0;
  189. int i;
  190. for (i = 0; i < ARRAY_SIZE(nokia_timings); i++) {
  191. err |= sdrc_timings(i, nokia_timings[i].rate,
  192. nokia_timings[i].data);
  193. if (err)
  194. pr_err("%s: error with rate %ld: %d\n", __func__,
  195. nokia_timings[i].rate, err);
  196. }
  197. return err ? NULL : nokia_sdrc_params;
  198. }