s3c2412-iotiming.c 7.6 KB

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