iotiming-s3c2412.c 7.3 KB

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