iotiming-s3c2412.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2006-2008 Simtec Electronics
  3. * http://armlinux.simtec.co.uk/
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2412/S3C2443 (PL093 based) IO timing support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ioport.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/device.h>
  19. #include <linux/delay.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/amba/pl093.h>
  24. #include <asm/mach/arch.h>
  25. #include <asm/mach/map.h>
  26. #include <plat/cpu.h>
  27. #include <plat/cpu-freq-core.h>
  28. #include <mach/s3c2412.h>
  29. #define print_ns(x) ((x) / 10), ((x) % 10)
  30. /**
  31. * s3c2412_print_timing - print timing infromation via printk.
  32. * @pfx: The prefix to print each line with.
  33. * @iot: The IO timing information
  34. */
  35. static void s3c2412_print_timing(const char *pfx, struct s3c_iotimings *iot)
  36. {
  37. struct s3c2412_iobank_timing *bt;
  38. unsigned int bank;
  39. for (bank = 0; bank < MAX_BANKS; bank++) {
  40. bt = iot->bank[bank].io_2412;
  41. if (!bt)
  42. continue;
  43. printk(KERN_DEBUG "%s: %d: idcy=%d.%d wstrd=%d.%d wstwr=%d,%d"
  44. "wstoen=%d.%d wstwen=%d.%d wstbrd=%d.%d\n", pfx, bank,
  45. print_ns(bt->idcy),
  46. print_ns(bt->wstrd),
  47. print_ns(bt->wstwr),
  48. print_ns(bt->wstoen),
  49. print_ns(bt->wstwen),
  50. print_ns(bt->wstbrd));
  51. }
  52. }
  53. /**
  54. * to_div - turn a cycle length into a divisor setting.
  55. * @cyc_tns: The cycle time in 10ths of nanoseconds.
  56. * @clk_tns: The clock period in 10ths of nanoseconds.
  57. */
  58. static inline unsigned int to_div(unsigned int cyc_tns, unsigned int clk_tns)
  59. {
  60. return cyc_tns ? DIV_ROUND_UP(cyc_tns, clk_tns) : 0;
  61. }
  62. /**
  63. * calc_timing - calculate timing divisor value and check in range.
  64. * @hwtm: The hardware timing in 10ths of nanoseconds.
  65. * @clk_tns: The clock period in 10ths of nanoseconds.
  66. * @err: Pointer to err variable to update in event of failure.
  67. */
  68. static unsigned int calc_timing(unsigned int hwtm, unsigned int clk_tns,
  69. unsigned int *err)
  70. {
  71. unsigned int ret = to_div(hwtm, clk_tns);
  72. if (ret > 0xf)
  73. *err = -EINVAL;
  74. return ret;
  75. }
  76. /**
  77. * s3c2412_calc_bank - calculate the bank divisor settings.
  78. * @cfg: The current frequency configuration.
  79. * @bt: The bank timing.
  80. */
  81. static int s3c2412_calc_bank(struct s3c_cpufreq_config *cfg,
  82. struct s3c2412_iobank_timing *bt)
  83. {
  84. unsigned int hclk = cfg->freq.hclk_tns;
  85. int err = 0;
  86. bt->smbidcyr = calc_timing(bt->idcy, hclk, &err);
  87. bt->smbwstrd = calc_timing(bt->wstrd, hclk, &err);
  88. bt->smbwstwr = calc_timing(bt->wstwr, hclk, &err);
  89. bt->smbwstoen = calc_timing(bt->wstoen, hclk, &err);
  90. bt->smbwstwen = calc_timing(bt->wstwen, hclk, &err);
  91. bt->smbwstbrd = calc_timing(bt->wstbrd, hclk, &err);
  92. return err;
  93. }
  94. /**
  95. * s3c2412_iotiming_debugfs - debugfs show io bank timing information
  96. * @seq: The seq_file to write output to using seq_printf().
  97. * @cfg: The current configuration.
  98. * @iob: The IO bank information to decode.
  99. */
  100. void s3c2412_iotiming_debugfs(struct seq_file *seq,
  101. struct s3c_cpufreq_config *cfg,
  102. union s3c_iobank *iob)
  103. {
  104. struct s3c2412_iobank_timing *bt = iob->io_2412;
  105. seq_printf(seq,
  106. "\tRead: idcy=%d.%d wstrd=%d.%d wstwr=%d,%d"
  107. "wstoen=%d.%d wstwen=%d.%d wstbrd=%d.%d\n",
  108. print_ns(bt->idcy),
  109. print_ns(bt->wstrd),
  110. print_ns(bt->wstwr),
  111. print_ns(bt->wstoen),
  112. print_ns(bt->wstwen),
  113. print_ns(bt->wstbrd));
  114. }
  115. /**
  116. * s3c2412_iotiming_calc - calculate all the bank divisor settings.
  117. * @cfg: The current frequency configuration.
  118. * @iot: The bank timing information.
  119. *
  120. * Calculate the timing information for all the banks that are
  121. * configured as IO, using s3c2412_calc_bank().
  122. */
  123. int s3c2412_iotiming_calc(struct s3c_cpufreq_config *cfg,
  124. struct s3c_iotimings *iot)
  125. {
  126. struct s3c2412_iobank_timing *bt;
  127. int bank;
  128. int ret;
  129. for (bank = 0; bank < MAX_BANKS; bank++) {
  130. bt = iot->bank[bank].io_2412;
  131. if (!bt)
  132. continue;
  133. ret = s3c2412_calc_bank(cfg, bt);
  134. if (ret) {
  135. printk(KERN_ERR "%s: cannot calculate bank %d io\n",
  136. __func__, bank);
  137. goto err;
  138. }
  139. }
  140. return 0;
  141. err:
  142. return ret;
  143. }
  144. /**
  145. * s3c2412_iotiming_set - set the timing information
  146. * @cfg: The current frequency configuration.
  147. * @iot: The bank timing information.
  148. *
  149. * Set the IO bank information from the details calculated earlier from
  150. * calling s3c2412_iotiming_calc().
  151. */
  152. void s3c2412_iotiming_set(struct s3c_cpufreq_config *cfg,
  153. struct s3c_iotimings *iot)
  154. {
  155. struct s3c2412_iobank_timing *bt;
  156. void __iomem *regs;
  157. int bank;
  158. /* set the io timings from the specifier */
  159. for (bank = 0; bank < MAX_BANKS; bank++) {
  160. bt = iot->bank[bank].io_2412;
  161. if (!bt)
  162. continue;
  163. regs = S3C2412_SSMC_BANK(bank);
  164. __raw_writel(bt->smbidcyr, regs + SMBIDCYR);
  165. __raw_writel(bt->smbwstrd, regs + SMBWSTRDR);
  166. __raw_writel(bt->smbwstwr, regs + SMBWSTWRR);
  167. __raw_writel(bt->smbwstoen, regs + SMBWSTOENR);
  168. __raw_writel(bt->smbwstwen, regs + SMBWSTWENR);
  169. __raw_writel(bt->smbwstbrd, regs + SMBWSTBRDR);
  170. }
  171. }
  172. static inline unsigned int s3c2412_decode_timing(unsigned int clock, u32 reg)
  173. {
  174. return (reg & 0xf) * clock;
  175. }
  176. static void s3c2412_iotiming_getbank(struct s3c_cpufreq_config *cfg,
  177. struct s3c2412_iobank_timing *bt,
  178. unsigned int bank)
  179. {
  180. unsigned long clk = cfg->freq.hclk_tns; /* ssmc clock??? */
  181. void __iomem *regs = S3C2412_SSMC_BANK(bank);
  182. bt->idcy = s3c2412_decode_timing(clk, __raw_readl(regs + SMBIDCYR));
  183. bt->wstrd = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTRDR));
  184. bt->wstoen = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTOENR));
  185. bt->wstwen = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTWENR));
  186. bt->wstbrd = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTBRDR));
  187. }
  188. /**
  189. * bank_is_io - return true if bank is (possibly) IO.
  190. * @bank: The bank number.
  191. * @bankcfg: The value of S3C2412_EBI_BANKCFG.
  192. */
  193. static inline bool bank_is_io(unsigned int bank, u32 bankcfg)
  194. {
  195. if (bank < 2)
  196. return true;
  197. return !(bankcfg & (1 << bank));
  198. }
  199. int s3c2412_iotiming_get(struct s3c_cpufreq_config *cfg,
  200. struct s3c_iotimings *timings)
  201. {
  202. struct s3c2412_iobank_timing *bt;
  203. u32 bankcfg = __raw_readl(S3C2412_EBI_BANKCFG);
  204. unsigned int bank;
  205. /* look through all banks to see what is currently set. */
  206. for (bank = 0; bank < MAX_BANKS; bank++) {
  207. if (!bank_is_io(bank, bankcfg))
  208. continue;
  209. bt = kzalloc(sizeof(struct s3c2412_iobank_timing), GFP_KERNEL);
  210. if (!bt) {
  211. printk(KERN_ERR "%s: no memory for bank\n", __func__);
  212. return -ENOMEM;
  213. }
  214. timings->bank[bank].io_2412 = bt;
  215. s3c2412_iotiming_getbank(cfg, bt, bank);
  216. }
  217. s3c2412_print_timing("get", timings);
  218. return 0;
  219. }
  220. /* this is in here as it is so small, it doesn't currently warrant a file
  221. * to itself. We expect that any s3c24xx needing this is going to also
  222. * need the iotiming support.
  223. */
  224. void s3c2412_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
  225. {
  226. struct s3c_cpufreq_board *board = cfg->board;
  227. u32 refresh;
  228. WARN_ON(board == NULL);
  229. /* Reduce both the refresh time (in ns) and the frequency (in MHz)
  230. * down to ensure that we do not overflow 32 bit numbers.
  231. *
  232. * This should work for HCLK up to 133MHz and refresh period up
  233. * to 30usec.
  234. */
  235. refresh = (cfg->freq.hclk / 100) * (board->refresh / 10);
  236. refresh = DIV_ROUND_UP(refresh, (1000 * 1000)); /* apply scale */
  237. refresh &= ((1 << 16) - 1);
  238. s3c_freq_dbg("%s: refresh value %u\n", __func__, (unsigned int)refresh);
  239. __raw_writel(refresh, S3C2412_REFRESH);
  240. }